Macro Example: Formatting

Here is a macro for formatting a data table. In the worksheet, we have two data ranges. We record the process of formatting the first data range, and then we use the same macro to format the second data range.

Step 1: In the "Developer" Tab from the Ribbon, check the button of "Use Relative References" and click the "Record Macro";

Step 2: In the new window, type the macro name, and fill in the three other sections (optional).

  • Please note that the first character of the macro name must be a letter. You can use the underscore, but the space is not allowed in the macro name.

Step 3: Click "OK" to save the macro. In the worksheet, start formatting the first table as below. Click "Stop Recording" when you complete the formatting.

  • Add borders to the first table;
  • Bold the headers;
  • Set the header background color as blue;
  • Format the phone with the format "xxx-xxx-xxxx";
  • Format the income with the dollar sign.

We can play the recorded macro in the second data range to have the same format.

Step 1: Move the cursor to the first cell in the second data range, and click "Macro" in the "Developer" tab.

Step 2: From the macro list, select the proper macro and click "Run" to play the macro. You will have the second data range formatted in the same way as the first data range.

Please note that you need to put the cursor in the location when you record the macro.

Leave a Reply