Chapter 3
Editing Recorded Macros

In this chapter, you'll use the Visual Basic Editor to edit the Word and Excel macros you recorded with the Macro Recorder in Chapter 1, “Recording and Running Macros in the Office Applications.” In addition, you'll create a new macro in PowerPoint and see how to edit it. Even if you're working with an application that doesn't include the Macro Recorder (such as PowerPoint), you may still want to read through this chapter because it shows you how to use some of the key editing features of the Visual Basic Editor.

There are three reasons for using the Visual Basic Editor to work with macros:

  • First, to fix any problems in the behavior of a macro you recorded. For example, if you accidentally hit the Enter key while recording the macro, the macro will keep performing that wrong instruction every time you run it unless you remove or change the instruction. You would want to use the Editor to delete this line of code in your macro:
    Selection.TypeParagraph

    (Alternatively, it's sometimes easier to just rerecord a macro than it is to repair it manually.)

  • Second, to modify existing code or add additional code to a macro to make it behave differently. This is a great way to get started learning the VBA language. And sometimes by just making relatively small or simple changes to a recorded macro, you can greatly increase its power and flexibility.
  • Third, to create a new macro by writing code in the Visual Basic Editor instead of recording the macro. You can write a new macro from scratch or paste in parts of an existing macro, as appropriate.

Testing a Macro in the Visual Basic Editor

If a macro fails when you try to run it from the host application, the quickest way to find out what's going wrong is to open the macro in the Visual Basic Editor, run it, and see where in the code it fails:

  1. In the host application, click Developer ➢ Macros or choose View ➢ Macros to display the Macros dialog box.
  2. Select the buggy macro, and then click the Edit button.

    The host application opens an instance of the Visual Basic Editor and displays the macro for editing.

  3. Start the macro running by pressing F5.

    Alternatively, you could choose Run ➢ Run Sub/UserForm or click the Run Sub/UserForm button (a green arrow) on the Standard toolbar in the Visual Basic Editor (see Figure 3.1).

    Illustration of the Standard toolbar to start running the code by clicking the Run Sub/UserForm button.

    Figure 3.1 Click the Run Sub/UserForm button on the Standard toolbar to start running the code.

    If the macro encounters an error and halts execution (goes into Break mode), VBA displays an error-message box onscreen and selects the offending statement in the Code window (displays the buggy code as white letters on a blue background).

  4. You can then edit the statement to fix the problem. Once you've done so, step through the macro as described in the next section.

Stepping Through a Macro

To see exactly what a macro does (or what it does wrong), you can step through the macro—go through the macro, executing one command at a time, like watching a football replay in slow motion. This way you can see the effect of each command. Stepping through a macro can be time-consuming, but it's one of the best ways to identify problems and fix them. You can, for example, switch at any time to the affected document, presentation, or spreadsheet to see the effect of the just-executed line in the code.

Usually debugging is a matter of finding out where in the code something goes wrong. Although you generally already know what goes wrong, you must find the location of the problem in your code. Then you can determine how the error happens.

To step through a macro, follow these steps:

  1. Open the host application, and then open the macro for editing by pressing Click Developer ➢ Macros.
  2. Select the macro, and then click the Edit button.

    Sometimes it's helpful to arrange the Visual Basic Editor window and the host application's window so that you can see them both simultaneously. Either arrange the windows manually or use a Windows command to do so. For example, stack the windows by right-clicking in open space on the Windows Taskbar and choosing Show Windows Stacked from the context menu.

    Alternatively, you can select Show Windows Side By Side. If you have any other applications currently running, minimize them so they won't be included in your stack. (If you have two monitors, you can dedicate one to the Editor and one to the application.) In Windows 8, the quickest way to display two windows is to

    1. Drag one of them to the far left.
    2. Drop it, and it will snap to that location and automatically resize so it takes up only 50 percent of the screen.
    3. Drag the other window to the right.

    In Windows 10, press Windows Key+←, or Windows Key+→.

  3. Now set up any conditions the macro expects.

    Perhaps you need to have a document open. To run properly, a macro that applies a style to the current paragraph requires that a paragraph is actually available. Hence, the document must be open and a paragraph available. Also, the blinking vertical line (insertion point) must be located somewhere within the paragraph in your document. You place the insertion point by simply clicking within the text where you want it located.

  4. Click somewhere in the macro code.

    The location of the blinking insertion point in the Editor is how the Editor determines which macro you want to execute when you press F5 or F8.

  5. Press F8 to step through the macro command by command.

    Each time you press F8, one line of your VBA code will be executed. The Visual Basic Editor highlights in yellow each command as it's executed, and you can switch to the application to watch the effect and try to catch errors.

Figure 3.2 provides an example of stepping through a macro recorded in Word.

Illustration providing an example of stepping through a macro recorded in Word.

Figure 3.2 Stepping through a macro recorded in Word

You'll learn about debugging macros in detail in Chapter 17, “Debugging Your Code and Handling Errors.” However, let me briefly introduce two additional important techniques that can help you locate bugs in your macros: setting breakpoints and commenting out lines.

Setting Breakpoints

A breakpoint can be specified for a line of code to tell VBA to stop executing the macro there. By using a breakpoint, you can first press F5 to quickly execute known functional parts of a macro at full speed, and then the Editor automatically stops at the breakpoint. You put a breakpoint just before where you suspect a bug is located in the code. That way, you don't have to step through all your code. You can execute the macro at normal, rapid speed—but then halt near the suspicious location and begin pressing F8 to step through the code, executing it slowly, statement by statement, to closely observe the behaviors. You can set as many breakpoints as you want.

To toggle a breakpoint on or off, right-click in a line of executable code (not a comment line, described in the following section) and choose Toggle ➢ Breakpoint from the context menu or click the Toggle Breakpoint button on the Edit toolbar. Even easier, just click in the gray margin-indicator bar to the left of the line of code.

A line of code on which you set a breakpoint is shaded red by default. The breakpoint itself is designated by a red circle in the margin indicator bar (see Figure 3.3).

Screenshot indicating to use a breakpoint (the dark circle that appears in the margin indicator bar) to stop code execution at a line of the user's choice.

Figure 3.3 Use a breakpoint (the red circle that appears in the margin indicator bar) to stop code execution at a line of your choice.

Commenting Out Lines

Like all programming languages, VBA lets you add comments to your code so that it's easier to understand. Comments can be invaluable both while you're creating code and when you're revisiting your own code long after you've forgotten what it does—or, worse, when you're trying to figure out what someone else's code does.

Comments are ignored when your macro executes—they are for the programmer's information only.

However, there's another use for commenting. You can also comment out lines of code to prevent the Visual Basic Editor from executing them. In other words, comments are normally just notes to self that are not part of the macro proper. They are not written in VBA, nor does the Editor pay any attention to them when examining the code for errors, or executing the code.

However, comments are also used for debugging purposes as well. Sometimes while debugging you'll want to comment out a line or lines of executable code in your macro. That way during execution, the commented out code is simply not executed. It's ignored.

Why do this? It can be a useful technique for temporarily skipping over suspect lines of code without actually removing them from the macro. Then you run the code and see what the difference is with the commented lines ignored. If the bug goes away, it's probably located within the lines that are commented out.

For example, if a macro wrongly deletes some text in a document, but after being commented out, it doesn't do that—you've found the bug!

To comment out a line manually, type an apostrophe (') at the very beginning of the line. Alternatively, you can use the Rem command instead of the apostrophe. (Rem is short for remark, and comment lines are sometimes called remark lines.) To uncomment the line manually, just delete the apostrophe or Rem.

The Visual Basic Editor provides convenient Comment Block and Uncomment Block commands for commenting out multiple lines automatically. Drag to select the target lines of code, and then click the Comment Block button on the Edit toolbar to place an apostrophe at the beginning of each line; to uncomment a line or a group of selected lines, click the Uncomment Block button, and the Visual Basic Editor removes an apostrophe from each line.

The Comment Block command employs only apostrophes, not Rem lines. If you prefer to use Rem, you must comment and uncomment lines manually. Few people, though, use Rem these days.

Stepping Out of a Macro

Once you've identified and fixed the problem with a macro, you probably won't want to step through the rest of the macro command-by-command.

To run the rest of the macro, you can again press the F5 key. Alternatively, you can click the Run Sub/UserForm button on the Standard toolbar or the Debug toolbar (see Figure 3.4), or you can choose Run ➢ Continue.

Illustration of the Debug toolbar that contains commands for running code, stepping into it and out of it, and displaying key windows for debugging.

Figure 3.4 The Debug toolbar contains commands for running code, stepping into it and out of it, and displaying key windows for debugging.

If you want to run only the rest of only the current macro, and then return to stepping through any other macro that called (triggered) the current one, use the Step Out command. The Step Out command finishes executing the current macro or procedure at full speed, but if the code then continues with another procedure, the Visual Basic Editor reverts to Break mode so you can examine that procedure's code. We'll explore what it means to call procedures later in this book.

To issue the Step Out command, press Ctrl+Shift+F8, click the Step Out button on the Debug toolbar, or choose Debug ➢ Step Out.

Editing a Word Macro

Now try editing the Transpose_Word_Right macro that you recorded in Word in Chapter 1, and also build another macro out of it. To begin, open the macro in the Visual Basic Editor:

  1. Start Word if it's not already running, or activate it.
  2. Click Developer ➢ Macros.
  3. Select the Transpose_Word_Right macro, and then click the Edit button.

In the Code window, you should see code similar to Listing 3.1, except for the line numbers, which I'm using here to identify the lines of code.

Here's what the macro does:

  • Line 1 starts the macro with the Sub Transpose_Word_Right() statement, and line 15 ends the macro with the End Sub statement. The Sub and End Sub lines mark the beginning and end of the macro (as they do any macro).
  • Lines 2 and 6 are blank comment lines the Macro Recorder inserts to make your macro easier to read. You can use any number of blank lines or blank comment lines in a macro to help separate statements into groups. (A blank line doesn't have to be commented out—it can just be blank—but the Macro Recorder has added commenting to these blank lines to make it clear what they are.)
  • Lines 3 through 5 are comment lines that contain the name of the macro and its description. The Macro Recorder entered these lines from the information you typed into the Record Macro dialog box.
  • Line 7 records the first keystroke of the F8 key, which starts Extend mode—a way of selecting text in a Word document.
  • Line 8 records the second keystroke of the F8 key, which continues Extend mode and thereby selects the current word.
  • Line 9 records the keystroke of the Esc key, which cancels Extend mode.
  • Line 10 records the Cut command, which cuts the selection (in this case, the selected word) to the Clipboard.
  • Line 11 records the Ctrl+→ command, which moves the insertion point one word to the right.
  • Line 12 records the Paste command, which pastes the selection into the document at the current position of the insertion point. Whatever formatting was originally applied to the selection is retained (rather than applying the formatting in effect at the new location).
  • Line 13 records the Ctrl+← command, which moves the insertion point one word to the left.

Stepping Through the Transpose_Word_Right Macro

Now try stepping through this macro in Break mode using the Step Into command:

  1. Arrange your screen so you can see both the active Word window and the Visual Basic Editor window (for example, by right-clicking the Taskbar and choosing Show Windows Stacked from the context menu or by snapping each window to a side of the screen).
  2. Click in the Visual Basic Editor, and then click to place the blinking insertion point at the start (on the Sub command) of the Transpose_Word_Right macro in the Code window.
  3. Press F8 to step through the code one active line at a time.

    You'll notice that VBA skips over the blank lines and the comment lines because they're supposed to be ignored by the computer during execution. VBA highlights the current statement each time you press F8, and you see the actions taking place in the Word window.

The Visual Basic Editor leaves Break mode when it reaches the end of the macro (in this case, when you press F8 to execute the End Sub statement in line 15). The Editor returns to Design mode. Recall that you can also exit Break mode at any time by clicking the Reset button (blue square) on the Standard or the Debug toolbar, or by choosing Run ➢ Reset. Unfortunately there is no keyboard shortcut for this.

Running the Transpose_Word_Right Macro

If the macro works fine when you step through it, you may also want to run it at normal speed from the Visual Basic Editor. Just press F5. In Break mode, F5 executes the macro, starting from the current instruction (where the insertion point is located).

Creating a Transpose_Word_Left Macro

Now let's try modifying this macro. We'll create a Transpose_Word_Left macro by making minor adjustments to the Transpose_Word_Right macro. Follow these steps:

  1. In the Code window, select all the code for the Transpose_Word_Right macro, from the Sub Transpose_Word_Right() line to the End Sub line.

    You can select in three ways:

    • By dragging with the mouse
    • By holding down Shift and using the arrow keys to extend the selection
    • By positioning the insertion point at one end of the macro and then Shift+clicking the other end
  2. Copy the code by issuing a Copy command (for example, by right-clicking and choosing Copy from the context menu or by pressing Ctrl+C or Ctrl+Insert).
  3. Click to move the insertion point to the line below the End Sub statement for the Transpose_Word_Right macro in the Code window.
  4. Paste the code by issuing a Paste command (by right-clicking and choosing Paste from the context menu or by pressing Ctrl+V or Shift+Insert).

    The Visual Basic Editor automatically enters a horizontal line between the End Sub statement for the Transpose_Word_Right macro and the new macro you've pasted.

  5. Change the name of the second Transpose_Word_Right macro to Transpose_Word_Left by editing the Sub line:
    Sub Transpose_Word_Left()
  6. Edit the comment lines at the beginning of the macro accordingly—for example:
    'Transpose_Word_Left Macro
    'Transposes the current word with the word to its left. _
    'Created 5/15/19 by Nanci Luz Selest-Gomes.
  7. Now all you need to do is replace the MoveRight method with the MoveLeft method.

    This will move the insertion point one word to the left instead of one word to the right. While you could do that by typing the correction or by using Cut and Paste to replace the Selection.MoveRight line with the commented-out Selection.MoveLeft line, try using the List Properties/Methods feature instead. Just for practice, follow these steps:

    1. Click to place the insertion point in the word MoveRight.
    2. Click the List Properties/Methods button on the Edit toolbar to display the list of properties and methods.

      It's the first button on the far left. Or just press Ctrl+J. (If the Edit toolbar isn't visible, right-click one of the existing toolbars and choose Edit from the context menu.)

    3. Double-click the MoveLeft method in the list to make it replace the MoveRight method in the code line.
  8. Now that you no longer need it, delete the line Selection.MoveLeft Unit:=wdWord, Count:=1 from the end of the macro.

You should end up with a macro that looks like Listing 3.2.

Try stepping through this macro to make sure it works. If it does, you're ready to save it—and perhaps to create a Quick Access Toolbar button, or keyboard shortcut, for it—if you plan to use it in your writing. And you want a quick way to execute it.

Saving Your Work

When you finish working with this or any other macro, choose File ➢ Save (Ctrl+S) from the Visual Basic Editor to save the document or template that contains the macro and the changes you've made to it. Then press Alt+Q or choose File ➢ Close And Return To Microsoft Word to close the Visual Basic Editor and return to Word.

Editing an Excel Macro

In the following sections, you'll edit the Excel macro that you recorded in Chapter 1 . This time, you won't create a new macro—instead, you'll just add to the existing one.

Unhiding the Personal Macro Workbook

Before you can edit the Excel macro, you'll need to unhide the Personal Macro Workbook if it's currently hidden:

  1. Open the blank workbook.
  2. Click the View tab on the Ribbon.
  3. If the Unhide button is gray (disabled) in the Window group (see Figure 3.5), then no workbooks are hidden, including Personal, and you can skip down to step 4. However, if the Unhide button is black (enabled), click it to display the Unhide dialog box.
    Screenshot of an Excel worksheet indicating that the Personal Macro Workbook is not hidden.
    Figure 3.5 If gray, this means the Personal Macro Workbook isn't hidden. Skip step 3.
  4. Select PERSONAL.XLSM or PERSONAL.XLSB and click the OK button.

    If you stored the macro from Chapter 1 in another workbook, open that workbook before trying to proceed. To hide the Personal Macro Workbook again after editing the macro, click the Hide button on the Ribbon while the Personal Macro Workbook is active (visible).

Opening a Macro for Editing

Now take the following steps to open the Excel macro you recorded in Chapter 1 for viewing and editing:

  1. Click Developer ➢ Macros to display the Macros dialog box.
  2. Select the macro named Add_Months.
  3. Click the Edit button to display the macro for editing in the Visual Basic Editor.

Listing 3.3 shows code similar to what you should be seeing.

Here's what happens in the macro in Listing 3.3:

  • Line 1 starts the macro with the Sub New_Workbook_with_Months() statement, and line 13 ends the macro with the End Sub statement.
  • Lines 2 through 7 are comment lines that the Macro Recorder automatically adds. (The comment lines that are blank are merely to make the code easier to read. Delete any blank or comment lines you want. They'll have no effect on the behavior of the macro, although removing them could make it less readable in the Editor. It's your call.)
  • Line 3 is a comment line that gives the macro's name and describes it as a macro, and line 4 contains the description from the Record Macro dialog box.
  • Line 8 selects the Range object A1, making cell A1 active.
  • Line 9 enters Jan-2019 in the active cell. Notice that the Macro Recorder has stored the parsed date value rather than the text that you typed in (January 2019). Also, keep in mind that the date displayed in the cell may be in a different format than MMM.
  • Line 10 selects the Range object B1, making cell B1 active, and line 11 enters Feb-2019 in that cell.
  • Line 12 performs a default AutoFill operation on the range A1:L1.

Editing a Macro

Now modify the macro by following these steps:

  1. Select lines 8 through 12.
  2. Copy these lines by pressing Ctrl+C or right-clicking in the selection and choosing Copy from the context menu.
  3. Press the Enter key to insert a new blank line just above the End Sub line.
  4. Paste the copied lines by pressing Ctrl+V, choosing Edit ➢ Paste, or right-clicking at the insertion point and choosing Paste from the context menu.

Your new macro should look like Listing 3.4.

Now, change the macro to display a second row of autofilled values by taking the following steps:

  1. Change line 13 to select cell A2 instead of cell A1:
    Range("A2").Select
  2. Change line 14 so that it enters the value 1 instead of Jan-2019:
    ActiveCell.FormulaR1C1 = 1
  3. Change line 15 to select cell L2 instead of cell B1:
    Range("L2").Select
  4. Change line 16 so that it enters the value 12 instead of Feb-2019:
    ActiveCell.FormulaR1C1 = 12
  5. Change line 17 so that it performs the AutoFill operation on the range A2:L2, and change the fill type to xlFillSeries:
    Selection.AutoFill Destination:=Range("A2:L2"), Type:=xlFillSeries
  6. Click the Save button or choose File ➢ Save to save the changes you made.

The macro should now read like Listing 3.5.

To see this in action, go back to Excel and choose File ➢ New Blank Workbook. Make sure you're seeing this new workbook, not Personal (this makes it the active workbook, the one where our macro will do its job). Choose Developer ➢ Macros and double-click the Step Into button. Now press F8 to step through the macro and watch what happens: As before it enters the months, but then it enters the values 1 through 12 in the second row of cells. This one is fun to watch on a split screen because you watch the cells fill with data as you step through it.

Editing a PowerPoint Macro

In this section, you'll edit a PowerPoint macro. PowerPoint has no Macro Recorder, so you'll either have to type in the code for the following example or, better, just copy and paste it from this book's web page at www.sybex.com/go/masteringvba2019.

Start by opening the PowerPoint Visual Basic Editor:

  1. Open PowerPoint, and choose the blank presentation template.
  2. Now add a shape by clicking the Insert tab on the Ribbon and then clicking the Shapes icon in the Illustrations section.
  3. Click to select a rectangle shape of your choice, and click somewhere in the slide to create it.

    This will be object 1 in the Shapes collection, so we can refer to it in the code like this:

    ActiveWindow.Selection.SlideRange.Shapes(1).Select
  4. Open the PowerPoint Visual Basic Editor by clicking Developer ➢ Visual Basic.
  5. Create a new, empty module by choosing Insert ➢ Module in the Editor.

    Now you're ready to add some code.

  6. Type in (or paste from this book's web page) the code shown in Listing 3.6.

Press F5 to run the macro and see what it does to PowerPoint.

Here's what happens in the macro:

  • Line 1 starts the macro, and line 44 ends it.
  • Lines 2 and 4 are blank comment lines used to set off the description of the macro, which appears in line 3.
  • Line 5 adds the slide to the presentation. This statement is a little complicated, but don't worry about it too much just yet. For now, note two things:
    • First, the statement uses the Add method with the Slides collection object to add a slide to the collection (in other words, to create a new slide in this case). This is similar to the way the Excel macro (explored earlier in this chapter) used the Add method to add a workbook to its Workbooks collection.
    • Second, the layout of the slide is ppLayoutText, the VBA constant for the Text slide layout that PowerPoint uses for a default new slide.
  • Line 6 applies the Title layout (ppLayoutTitle) that you chose when recording the macro. (If you chose a different slide layout, you'll see a different constant than ppLayoutTitle.)
  • Line 7 selects the first shape in the Shapes collection on the active slide. (For the moment, don't worry about how you get to the active slide.)
  • Lines 8 to 11 are a With block. This block begins with a With statement that specifies properties or behaviors (methods) for the shape that has been selected (ActiveWindow.Selection.ShapeRange). A With statement is a way of simplifying object references, and everything between the With statement and the End With statement refers to the objects that the With statement first mentions. In this case, line 9 uses the IncrementLeft method with a negative value to move the shape to the left, and line 10 uses the IncrementTop method with a negative value to move the shape up the slide.
  • Line 13 selects the first shape in the Shapes collection, and line 14 selects the TextRange object in the TextFrame object in the shape. When you're working interactively, PowerPoint makes this selection process seamless: You click in a shape displaying the legend “Click To Add Title” (or whatever), and PowerPoint selects the text range in the shape's text frame—but all you see is that the text in the shape becomes selected. In VBA, you have to go through a couple of unseen layers in the object model before getting to the text.
  • When you select the placeholder text, PowerPoint gets rid of it. The same thing happens when you select the placeholder text via VBA. So line 15 makes a new selection at the beginning of the first character in the text range. The Length of the selection is 0, meaning that the selection is collapsed to an insertion point rather than containing any characters. Line 16 starts a With statement that continues until line 30. The With ActiveWindow.Selection.TextRange statement in line 16 lets line 17 reference the Text property of the TextRange object in the ActiveWindow object's Selection object much more simply (instead of ActiveWindow.Selection.TextRange.Text), and it lets line 18 reference the Font property of the TextRange object in the Selection object in the ActiveWindow object easily (instead of ActiveWindow.Selection.TextRange.Font).
  • Line 17 sets the Text property of the ActiveWindow.Selection.TextRange object to the text typed.
  • Line 18 then begins a nested With statement that sets the properties of the Font object for the TextRange object. Line 19 sets the Name property of the Font object to Arial; line 20 sets the Size property of the Font object to 44; line 21 sets the Bold property of the Font object to msoFalse, the Microsoft Office (mso) constant for False; and so on. These statements are not necessary for our purposes in this macro. But they're harmless, so you can leave them in your code or, if you like, delete this entire With block (as we'll do shortly). Line 29 ends the nested With statement.
  • Line 31 uses the Select method to select characters 1 through 42 in the text range. This is the same as pressing the Ctrl+Shift+Home key combination. Because this statement specifies the characters to select, you'll need to change it if you change the text that this macro inserts. (If you run the statement on a text range that has fewer than 42 characters, it will cause an error. If you run it on a text range that has more than 42 characters, it will select only the first 42 characters in the text range—not what you want.)
  • Line 32 begins another With statement that works with the Font object of the TextRange object. This With statement imitates what happens if the user opens and modifies the Font dialog box.
  • Line 43 ends the With statement, and line 44 ends the macro.

Reducing the Size of Your Macro

Now try editing this macro by slimming it down a little and changing the text it inserts:

  1. Delete the unnecessary With statement in lines 18 through 29.
  2. Delete line 30.
  3. Change lines 16 and 17 into a single statement without With:
    ActiveWindow.Selection.TextRange.Text = _
        “The quick brown dog jumped over the lazy fox"
  4. Now change the text that the new line 16 inserts. Type any text of your choice between the double quotation marks.
  5. Change line 31 to use the Select method on the text range rather than specifying which characters to select. Delete Characters(Start:=1, Length:=42) to leave this statement:
    ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select

    By specifying a range rather than a particular character count, you avoid the problem discussed earlier of having to count characters any time you change the message. Specifying a character count is called hard-coding and it's to be avoided whenever possible. If there's a way—as there is here with the TextRange property—just let the computer figure out the count rather than specifying it in your code.

  6. Click the Save button on the Standard toolbar or choose File ➢ Save to save the changes you've made to the presentation.
  7. In the Save As dialog box, locate the Save As Type drop-down list and change it from the default .pptx type (which cannot contain macros) to the .pptm type (which can).

You should now have code that reads like Listing 3.7.

Now step through the changed macro and make sure it works the way you expect.

Saving Your Work

When you finish working with this macro, choose File ➢ Save from the Visual Basic Editor to save the presentation that contains the macro and the changes you've made to it. Be sure to change the file type from the default .pptx to the macro-enabled .pptm file type. Then press Alt+Q or choose File ➢ Close And Return To Microsoft PowerPoint to close the Visual Basic Editor and return to PowerPoint.

The Bottom Line

  • Test a macro in the Visual Basic Editor. When you need to modify or debug a macro, the Visual Basic Editor is your best friend. It's filled with tools to make your job easier.
    • Master It Open a macro; then step through it to see if anything goes wrong.
  • Set breakpoints and use comments. Setting breakpoints allows you to press F5 to execute a macro, but forces the Editor to enter Break mode when execution reaches the line where the breakpoint resides. Comments help you understand the purpose of code—they describe it but are ignored during execution of the macro's code. “Commenting out” a line of code allows you to temporarily render it inactive to see what effect this has during execution. This is sometimes a good way to see if that line is causing the bug you're tracking down.
    • Master It Set a breakpoint in, and add a comment to, a macro.
  • Edit a recorded macro. Make some changes to a Word macro.
    • Master It With the Visual Basic Editor open, choose a macro and modify it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset