Monday, September 21, 2015

Recoding a macro

In this section, we will learn how to record a macro and see what codes the macro has. At first, we will record a simple macro that right aligns text in a cell. Enter the letters A to F in the A column of Excel:
the image: cited from HomeLearn

On the developer tab, see the Code panel click Record Macro.
the image: cited from HomeLearn

Then the Record Macro dialogue box will appear as following:
the image: cited from HomeLearn

We will name it TestMacro. Choose and click the Personal Macro Workbook then OK. Now the recording begins.
the image: cited from HomeLearn

Select the cells A1 to A6. Click on the Home tab on the Excel ribbon. See the Alignment panel and click the right-align option:

these images: cited from HomeLearn

Now see the coding wondow. You will find a new object (the recorded macro) on the white area on left. The new object is called Personal.XLSB. Click the plus symbol to expand the entry and you'll see some folders. Expand these as well:
the image: cited from HomeLearn

Double click Module1 and you will find the code of the macro which you have just recorded,
the image: cited from HomeLearn

To use this recorded macro, see the developer tab and find Macros. Click Macros and choose the macro you have added and click it.
the image: cited from HomeLearn. Though "stop recording" is being selected on the photo, choose Macros to use the macro that you've added.


Note that such a sub of macro which is created by recording tend to have uselessly long code. Uselessly long code takes uselessly long time to run. To make a short and efficient code for a Sub of  macro, we must write the code by ourselves.

References

Home Learn "Excel VBA programming" accessed on 21st September 2015.