| How to Save Personal
Macro Workbook
When Excel is Open |
|||||||||||
| Back
to Macro
List of Topics |
|||||||||||
| 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.)
The Paste Special command is a good example. This exercise records the Paste Special command in order to give us a command to work with (to break the command into more than one line). This exercise will also review the Paste Special command, review the =PROPER command, review autofill and other related VBA topics. This article is written in a step-by-step format and it introduces other Excel concepts as it proceeds. For the short version of this article, click here. Step-by-step:
2. Make column wider, if
necessary
3. Drag the cell with the
crosshair
cursor down to cell A10
4. In cell B5,
enter the function: =Proper(A5)
5. Copy B5 down to B10 6. At this point you desire
to copy the Proper text back into Column A.
7. Select the text in cells B5:B10 8. Click on Tools
(in the menu), then
9. Name the macro PasteSpecialValues
and click on OK
10. Click on the Copy button 11. Click on cell A5 12. Click on Edit
Paste Special (in the menu)
13. Click on Values 14. Click on OK
15. Press Escape
16. Click on the Stop
Recorder button
1 '
Explanation: 8 & 9 --This is the meat of this exercise. Note than Excel broke the command into two lines. The underscore character causes Excel to continue to read on the next line as if it was on the same line. The simple rule for breaking lines is to place the underscore character after a space that normally displays in the command. In some cases, such as in this macro, the macro recorder does this for you. By definition, the Paste Special command is long as it deals with all the items in the Paste Special dialog box. 11 --This command clears the clipboard. The marquee is no longer displayed around the data that is being copied. It is entered by Excel when you press ENTER to complete a Paste command or when you press Escape to clear the Clipboard. 1, 2, & 3 --All lines that begin with the apostrophe ( ' ) are comments and have no effect on the macro. 2 --The name of the
macro as entered by the recorder, but you can change it to read anything
you want or even delete the line.
To break the Paste Special command into several additional lines, making it even easier to read: 1. Place the cursor immediately in front of the P in Paste: Selection.PasteSpecial | Paste:=xlValues, Operation:=xlNone, _ The | represents the cursor. Make sure there is a space after PasteSpecial 2. Type the Underscore character
( _ ) and press Enter
3. Repeat these steps until this portion of the macro reads:
Paste:=xlValues, _ Operation:=xlNone, _ SkipBlanks:=False, _ Transpose:=False Review
Breaking up a macro text string into more than one line. Let's assume you desire for a macro to enter some text on the worksheet for you and you desire than all the text be placed in one cell (show on one line) in the spreadsheet, but as with the macro above you desire to look at it in two or more lines in the Module. Go to the bottom of your
Module and type the following macro.
1 Sub
EnterFootnote ( )
2 The
ActiveCell.FormulaR1C1
is the command to make an
2 Note the = , then a space , then the _ (underscore character) At this point, the macro is in two logical lines, the VBA command and the data associated with the command Select a sheet and run the macro to ensure it works. Now let's break the text
string into two lines. This must be done by concatenating two strings
together.
Edit the macro to look like: 1 Sub
EnterFootnote ( )
3 --Breaking a text string into two lines is done by concatenating two text strings together. Note the text string portion of line 3 ends with a double quote ( " ). The space before the double quote is the space that normally follows the word detailed. 3 --The ampersand
character ( & ) says that we are going to add
4 --This line is the remaining text, surrounded by quotes. When the macro runs, lines 3 & 4 are considered to be just one long text string. Select a sheet and run the
macro.
Drag the cell with the crosshair cursor
a cross hair in shape (some users call this shape plus) drag down the cell, you want the crosshair shape In this exercise, this invokes the autofill feature
and click on Stop Recorder recorded as well. If so, simply delete them from the recorded macro |
||||||||||
| Top |