Skip to main content

New Article

How to SUM by matching partial text in Excel

How to record a macro?

It is very difficult for new user to write a macro if they don't know how to write it. But it is very easy for them to record a macro. I will tell you the way here of recording a macro that simply center aligns text in a cell. To do this, you need to enter the text:

Image 1: Sample data

After entering these data, click on Record macro from Developer menu. This will show a dialog box. Write a name for your macro. I've used testMacro as macro name. And select the Personal Macro Workbook from Store macro in: box. I recommend to record your all macros which you want to test under Personal Macro Workbook, because it will not affect others workbook. You can use a description. After filling the dialog box it will looks like this:

Image 2: Settings of Record Macro

Now Select A1:A7 cell and Click on Center Align tool from Home menu. You don't need to hurry. It will not record time. It will record just your actions in written format.

Image 3: Center Align

After clicking on this tool you have finished your task. Now go back to your Developer tab and Click on Stop Recording button. Done it write? Now it's time to find the code. Remember that, you recorded the macro under Personal Macro Workbook.

Have a look at the coding window. If you haven't yet recorded a macro you should see a new item appear in the object window, VBAProject(PERSONAL.XLSB):

Image 4: VBAProject (PERSONAL.XLSB)

Click the plus symbol on left side of VBAProject (PERSONAL.XLSB) to expand the entry and you'll see some yellow folders. Expand these as well and Double Click on Module1 to show the codes.

 Image 5: Code of a recorded macro

You don't need to understand all the code at this early stage. But you can probably pick out a few things that make sense. Such as, 

.HorizontalAlignment = xlCenter 

You can change it to Right or Left instead of Center. By this way you can again record another macro and copy the code and apply in another workbook.