Excel Macros - Creation

Week 2

You can create a macro with Excel commands by recording the key strokes and mouse clicks, giving the macro a name and specifying how to store the macro. A macro thus recorded can be run with an Excel command.

Suppose you have to collect certain results repeatedly in the following format −

Format

Instead of creating the table each time, you can have a macro to do it for you.

Recording a Macro

To record a macro do the following −

Record

The Record Macro dialog box appears.

Record Macro

Remember that whatever key strokes and mouse clicks you do, will be recorded now.

Stop Recording

Your macro recording is completed.

The first step to click on a particular cell is important as it tells where exactly the macro has to start placing the recorded steps. Once you are done with the recording, you have to click Stop Recording to avoid recording of unnecessary steps.

Running a Macro

You can run the macro you have recorded any number of times you want. To run the macro, do the following −

Note the active cell. In our case, it is A1.

View

The Macro dialog box appears.

Macro Dialog Box

Only the macro that you recorded appears in the Macros list.

Macros List

You have discovered the magic wand that Excel provides you to save time on mundane tasks. You will observe the following −

You can run the macro in multiple worksheets with different active cells before running the macro and observe the same conditions as given above. Just keep a note of this and you will understand later in this tutorial why it has occurred so.

You can also have a macro recording that places your recorded steps in the active cell. You will learn how to do this as you progress in the tutorial.

Storing a Macro

You might wonder how to save the macros that are created. In this context you need to know −

As and when you create a macro, you can choose where to store that particular macro. You can do this in the Record Macro dialog box.

Click the box - Store macro in. The following three options are available −

Store Macro

This Workbook

This is the default option. The macro will be stored in your current workbook from where you created the macro.

New Workbook

This option, though available, is not recommended. You will be asking Excel to store the macro in a different new workbook and mostly it is not necessary.

Personal Macro Workbook

If you create several macros that you use across your workbooks, Personal Macro Workbook provides you with the facility to store all the macros at one place. You will learn more about this option in the next chapter.

Saving a Macro Enabled File

If you had chosen This Workbook as the option for storing the macro, you would need to save your workbook along with the macro.

Try to save the workbook. By default, you would be asking Excel to save the workbook as an .xls file. Excel displays a message saying that an Excel feature VB project cannot be saved in a macro free workbook, as shown below.

This Workbook

Note − If you click Yes, Excel will save your workbook as a macro free .xls file and your macro that you stored with This Workbook option will not get saved. To avoid this, Excel provides you an option to save your workbook as a macro-enabled workbook that will have .xlsm extension.

Save File

You will learn more about these in later chapters in this tutorial.