Posted by: Gomzy | January 9, 2007

Creating Simple Macros

This is the extracts of an presentation that I had prepared for an Training on Creation Simple MS Excel Macros.

Link to the file: Excel Macro Training.ppt

Introduction

  • We do a lot of repetitive tasks like copy pasting, formatting etc.
  • A macros help us to automate tedious or repetitive tasks with ease of a click of a button or shortcut key.
  • A macro is a set of instructions that tells Excel to perform one or more actions for you. Macros are like computer programs, but they run completely within Excel.
  • Macros can carry out sequence of actions much more quickly than you could do yourself.

Agenda

There are 2 ways to create a a macro: You can record it, or can build it by entering instructions in a module. Either way your instructions are encoded in the programming language MS VBA.

  • You’ll learn how to rerecord and execute a simple macro.
  • Then you will learn how to view the recorded macro and make it more useful by doing some simple editing.

Overview

The overall process for recording a macro consists of three steps.

  • Start the macro recorder and supply a name for the macro.
  • Perform the actions you want to record
  • Stop the macro recorder.

Using the Macro Recorder

Rather than type macros character by character, you can have Excel create a macro by recording the menu commands, keystrokes, and other action needed to accomplish a task.

Lets see how to create a macro to draw a boarder around a selected area.

  1. Select the small area in a blank work sheet
  2. Choose Tools>Macro>Record New Macro. Excel displays the Record Macro dialogue box.
  3. Assign Name to the macro.
  4. Assign a key combination to the macro by entering a letter.
  5. Store the macro in the currently active work book.
  6. Enter a description for the macro in the description box.
  7. To begin recording click OK. Excel displays the message Recording in the status bar and Stop Recording tool bar.
  8. Press Ctrl+1 and draw borders in the borders tab.
  9. Click the Stop Recording Macro button on the Stop Recording toolbar.

Running a Macro without Using a Keyboard Shortcut

  • You don’t have to know a macro’s key combination to run the macro. Instead, you can use the Macro dialog box:
  • Choose Tools>Macro, Macros to display the dialog box.
  • Select the name of the macro, and click Run.
  • You also can use the Macro dialog box to view and edit macros, as you’ll see in the next section.

Behind the Scenes: The VBA Environment

  • Now that you’ve recorded your macro, let’s find out what Excel did. When you clicked OK in the Record Macro dialog box, Excel created something called a module in the active workbook. As you drawn a table in the worksheet, Excel recorded your actions and inserted the corresponding VBA code in the module.
  • The new module doesn’t appear with the other sheets in the workbook; to view the module, choose Tools, Macro, Macros. Next, select the “Border” macro, and click the Edit button.
  • The Visual Basic Editor (VBE) starts up, and the module that contains the “Border” macro appears.
  • The first and last lines of the code act as the beginning and endpoints for the macro you’ve recorded; a Sub statement starts the macro and names it, and an End Sub statement ends the macro. You’ll notice that special VBA terms, called keywords, are displayed in dark blue.

Adding Code to an Existing Macro

  • Suppose you’ve recorded a macro that enters a series of labels, sets their font, and then draws a border around them. Then you discover that you forgot a step or that you recorded a step incorrectly—you chose the wrong border format, for example. What do you do?
  • To add code to an existing macro, you can record actions in a temporary macro and then transfer the code into the macro you want to change. For example, to the “Border” macro a step that sets font and border options for the table, follow these steps:

1. Choose Tools, Macro, Record New Macro. Excel presents the Record Macro dialog box. In the Macro Name box, enter MacroTemp and click OK. Excel displays the Stop Recording toolbar.

2. Choose Format, Cells, and click the Font tab. Select Trebuchet MS, 10-point, and Bold Italic. Then click OK to apply the formats.

3. Click the Stop Recording button on the Stop Recording toolbar.

4. Choose Tools, Macro, Macros. In the Macro dialog box, select MacroTemp and click Edit.

5. A window appears that contains the original macro you recorded plus the MacroTemp macro.

6. Select all the code inside the macro—from the line beginning With through the line beginning End With—and then choose Edit, Copy.

7. Scroll up to display the “Border” macro

8. Click at the penultimate that contains this statement:

End Sub

9. Press Enter to create a blank line. Then position the insertion point at the beginning of the blank line.

10. Choose Edit, Paste.

11. Scroll back down and delete the entire MacroTemp macro, from the Sub statement to the End Sub statement.

To test the macro, return to Excel (press Alt+F11 or select the Excel button on the taskbar). Clear the company name and address that you entered earlier when you recorded the macro. Then press Ctrl+Shift+A.

Using the Personal Macro Workbook

  • When you recorded the “Border” macro earlier, you placed the macro in a module that belongs to the active workbook. A macro that has been placed in a module is available only when the workbook containing the module is open.
  • To make a macro available at all times, store it in the Personal Macro Workbook. This workbook is normally hidden. you can unhide it by choosing Window, Unhide and selecting Personal in the Unhide dialog box.
  • If you don’t see the Personal file in the Unhide dialog box, or if the Unhide command is unavailable, you have not yet created a Personal Macro Workbook.
  • To create one, begin recording a macro, as described earlier in this chapter, andselect the Personal Macro Workbook option in the Record New Macro dialog box. Excel creates the Personal Macro Workbook and places its file (Personal.xls) in the XLStart folder.
  • Excel opens Personal.xls, as it does any other file in the XLStart folder, each time you start Excel.
  • Because the Personal Macro Workbook is always available when you work in Excel, it’s a good place to record macros that you want to be able to use in any workbook.

Going On from Here

  • You’ve learned how to create macros with the help of the macro recorder. As you learn more about the VBA programming language (a subject beyond the scope of this session), you’ll notice that the macro recorder often creates more code for a task than you really need. The recorder adds these lines because it didn’t (and couldn’t) know they weren’t necessary. You can edit them out without changing the functionality of the macro in any way.
  • As you move toward expertise in VBA, you will probably find yourself creating most of your code directly in the VBE, bypassing the recorder altogether

Conclusion

  • The best way to use the lessons learned today is to practice it. Use the macros in day to day office work.
  • Try to innovate and experiment on the things you have learned.
  • Share your acquired knowledge with others.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: