Macro Example: Sorting

Here is a macro for sorting a data table. In the worksheet, we have a data range. We record the process of sorting the data range, and then we use the same macro to sort the data range. Because we sort the same data range, we will record the macro with the "Use Relative References" off.

Step 1: In the "Developer" tab from the Ribbon, click the "Record Macro" with the button of "Use Relative References" off;

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 to customize the sorting with the two variables: Region from A to Z and Income from smallest to largest.

Step 4: Click "OK" when you complete, and click "Stop Macros".

For the test purpose, we will mess up the table and use the macro to sort the table.

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

Step 2: Select the proper macro and click "Run" to play the macro. You will have the data range sorted again.

Leave a Reply