Saturday, January 31, 2009

VBA Macro Excel Tutorial: Recording Macro Excel

Macro recorder

Macro recorder is Excel built-in tool to convert you excel actions into VBA code. Macro recorder is a powerful and useful tool, but it has some limitations:

  1. The macro recorder can’t record complex actions.
  2. Macro recorder can’t generate code that performs looping, conditional statement, and so on.

Recording Macro

In this example we will record a macro that will merge selected cells. To record the macro follow these steps:

  1. Open a new blank workbook.
  2. On any worksheet, select cells from A3 to C3.

  3. Click Tools –> Macro -> Record New Macro...

  4. In the Record Macro dialog box, type “SubMergeCells” in Macro name textbox to set the macro name. Then click Ok.

  5. In the formatting toolbars, click Merge and Center button. This action will merge cells from A3 to C3.

  6. To stop recording macro, click the Stop button.

Run the recorded macro

To execute the recorded macro, follow these steps:

  1. On any worksheet, select cells from D4 to G4.
  2. Click Tools -> Macro -> Macros... or press Alt+F8.
  3. In the Macro dailog box, select “SubMergeCells”. Click the Run botton to run the macro.
  4. Cells D4 to G4 now have been merged.

No comments:

Post a Comment