excel macro training

21
Microsoft Excel Training Creating Simple Macros

Upload: ashok-dwivedi

Post on 25-Sep-2015

35 views

Category:

Documents


4 download

DESCRIPTION

Excel Macro Training

TRANSCRIPT

  • Microsoft Excel TrainingCreating Simple Macros

  • 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.

  • AgendaThere 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. Youll 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 recordStop the macro recorder.effortefficiencytimesavequalityUDFXLMacro

  • Using the Macro RecorderRather 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.

  • Using the Macro Recorder contSelect the small area in a blank work sheetChoose Tools>Macro>Record New Macro. Excel displays the Record Macro dialogue box.Assign Name to the macro.Assign a key combination to the macro by entering a letter.

  • Using the Macro Recorder contStore the macro in the currently active work book.Enter a description for the macro in the description box.To begin recording click OK. Excel displays the message Recording in the status bar and Stop Recording tool bar.Press Ctrl+1 and draw borders in the borders tab.Click the Stop Recording Macro button on the Stop Recording toolbar.

  • Running a Macro Without Using a Keyboard Shortcut You dont have to know a macros 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 youll see in the next section.

  • Behind the Scenes: The VBA EnvironmentNow that youve recorded your macro, lets 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 doesnt 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.

  • Behind the Scenes: The VBA Environment contThe 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 youve recorded; a Sub statement starts the macro and names it, and an End Sub statement ends the macro. Youll notice that special VBA terms, called keywords, are displayed in dark blue.

  • Adding Code to an Existing MacroSuppose youve 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 incorrectlyyou 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:

  • Adding Code to an Existing Macro contChoose 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.Choose Format, Cells, and click the Font tab. Select Trebuchet MS, 10-point, and Bold Italic. Then click OK to apply the formats.Click the Stop Recording button on the Stop Recording toolbar.

  • Adding Code to an Existing Macro contChoose Tools, Macro, Macros. In the Macro dialog box, select MacroTemp and click Edit.A window appears that contains the original macro you recorded plus the MacroTemp macro.Select all the code inside the macrofrom the line beginning With through the line beginning End Withand then choose Edit, Copy.Scroll up to display the Border macro

  • Adding Code to an Existing Macro contClick at the penultimate that contains this statement:End SubPress Enter to create a blank line. Then position the insertion point at the beginning of the blank line.Choose Edit, Paste.Scroll back down and delete the entire MacroTemp macro, from the Sub statement to the End Sub statement.

  • Adding Code to an Existing Macro contTo 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 WorkbookWhen 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 dont 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.

  • Using the Personal Macro Workbook contTo 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, its a good place to record macros that you want to be able to use in any workbook.

  • Going On from HereYouve 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), youll notice that the macro recorder often creates more code for a task than you really need. The recorder adds these lines because it didnt (and couldnt) know they werent 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

  • ConclusionThe 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.

  • Where to Get More InformationVisit: www.gomzyxl.blogspot.comAny questions, queries, suggestions or comments?

  • Thank [email protected] 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