Excel Macro Training

21
Microsoft Excel Training Creating Simple Macros

description

Some time back i had prepared an presentation on creating a simple macro.

Transcript of Excel Macro Training

Page 1: Excel Macro Training

Microsoft Excel Training

Creating Simple Macros

Page 2: Excel Macro Training

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.

Page 3: Excel Macro Training

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.

Page 4: Excel Macro Training

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.

effort

efficiency

timesave

quality

UDF

XL

Macro

Page 5: Excel Macro Training

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.

Page 6: Excel Macro Training

Using the Macro Recorder cont…

1. Select the small area in a blank work sheet2. 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.

Page 7: Excel Macro Training

Using the Macro Recorder cont…

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.

Page 8: Excel Macro Training

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.

Page 9: Excel Macro Training

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.

Page 10: Excel Macro Training

Behind the Scenes: The VBA Environment cont…

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.

Page 11: Excel Macro Training

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:

Page 12: Excel Macro Training

Adding Code to an Existing Macro cont…

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.

Page 13: Excel Macro Training

Adding Code to an Existing Macro cont…

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

Page 14: Excel Macro Training

Adding Code to an Existing Macro cont…

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.

Page 15: Excel Macro Training

Adding Code to an Existing Macro cont…

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.

Page 16: Excel Macro Training

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.

Page 17: Excel Macro Training

Using the Personal Macro Workbook cont…

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.

Page 18: Excel Macro Training

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

Page 19: Excel Macro Training

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.

Page 20: Excel Macro Training

Where to Get More Information

Visit: www.gomzyxl.blogspot.com

Any questions, queries, suggestions or comments?

Page 21: Excel Macro Training

Thank You

[email protected]

Seemed the world was divided into good and bad people. The good ones slept better... while the bad ones seemed to enjoy the waking hours much more