Breaking a Macro Command into Two or More Lines
  The Short Version Back
 
  Home
In many cases a macro command line extends beyond the width of the screen and you desire to break the macro into two or more lines in order to see it all on the screen at one time. (Remember, macro commands by definition are one per line.)

This article shows you how to accomplish this. A separate article with the same title covers the same material in a more in-depth, step-by-step format. If you are somewhat comfortable with writing macros, this article will save you some time. 

After recording a macro to perform a Copy and  Paste Special Values  command, the procedure would look something like this:
 

1   '
2   'CopyPasteSpecial Macro
3   '
4   Sub CopyPasteSpecial()
5    Range("B5:B9").Select
6    Selection.Copy
7    Range("A5").Select
8    Selection.PasteSpecial Paste:=xlValues,Operation:=xlNone, _
9         SkipBlanks:=False, Transpose:=False
10   Range("A5").Select
11   Application.CutCopyMode = False
12  End Sub

The line numbers are shown for instructional purposes only.  Note the PasteSpecial command on lines 8 & 9.  By definition each macro command is on one line, however, where the command is long, it would be difficult to read as it would extend off the right edge of the screen. The macro recorder broke this line into two lines by placing  _  (the underscore character) at a break point.

You can do this on almost any macro by following these simple guidelines:
 

  • The _  (the underscore character) is the character used to break a line and have it be read as one continuous line.
  • The is placed after a space in the existing line.
  • If possible, place the _ after a space following a comma, these are natural breakpoints.


Exercise:
Revise the above macro so lines 8 & 9 read as follows:
 

    Selection.PasteSpecial _
       Paste:=xlValues, _
       Operation:=xlNone, _
       SkipBlanks:=False, _
       Transpose:=False

The best way to accomplish this is to place the mouse pointer after the comma and the trailing space.  Type the  _  (the underscore character) and then press Enter.  The first line doesn't have a comma, place the  _  after the space.  Pressing Enter places the text on the next line.  Using the Tab key and the space bar, line up the new lines.  If indented, it's easier to see that all the lines are a part of one long line. 

There are no hard and fast rules for this feature, if the macro doesn't work after breaking the line, undo your work and try it somewhat differently.  It's a good idea to test your macro before inserting the line breaks.  This could save time looking for a line break error when it was really a different issue.

This example was presented as the command is somewhat familiar and the macro recorder breaks it for you. Most of the time, you will be using this feature on macro typed in by you.
 

Breaking a macro text string into more than on line.

In this example you have the macro enter a long text string into a cell. 
You could enter the macro as follows:
 

ActiveCell.FormulaR1C1 = _
    "Please refer to Page 18 of this report for a detailed 
      explanation of the variances." 
 

Note that macro command is on two lines with the _  following the command. Due to screen width limitations, the text string (in quotes) is shown on two separate lines, but note the absence of the  _  (underscore).

To break the text string into two lines you need to break the one long text string into two text strings and concatenate them with the  _   between the two strings.  The  &  (ampersand) is the concatenation character. Concatenate and break into two lines as follows:
 

ActiveCell.FormulaR1C1 = _
    "Please refer to Page 18 of this report for a " & _
      "detailed explanation of the variances."
 

For purposes of this sample, concatenation is defined as the adding of two text strings together using the  &  (ampersand) character. The more accurate (and expanded) definition is:  the adding text strings and/or variables together using the  &  (ampersand) character. Variables are not enclosed in quotes. Variables are covered in other articles.

 

 
 
Top

 
 
 
 
 
 
 
 
 
 
 
 

 

solutions@info-stat.com