MS Excel is a powerful tool that offers a ton of options to its users. From storing data to creating automated dashboards, you can do it all in Excel and its spreadsheets.

If you want to use VBA to automate your sorting responsibilities in Excel, give these efficient easy-to-apply macros a try.

Download a Dummy Dataset

For starters, you can download a dummy dataset to work on your Excel macro skills.

Rest assured, once you get the hang of these macros and better understand how things work, you can shift the code to fit your own spreadsheets for work or school.

You can download the dataset used in this article if you’d like to follow along.

Download: Excel Dataset

1. Sorting One Column Using Excel Macros

Use this simple code to sort a data column within an Excel spreadsheet. If you downloaded the dummy dataset, you can try sorting column E (Units Sold).

Open a new Excel file and save it with an Excel Macro-Enabled Workbook (.xlsm) workbook type. This file will store the macro to sort your data from another file.

You will be controlling your file(s) from the macro file, which will interact with your workbooks separately.

Enter the following code:

Where:

Key1: Define the column(s) that you want to sort Order1: Ordering method (ascending/descending) Header: If your content has headers, this option will remain as xlYes. Alternatively, select xlNo.

The range will consist of the starting cell and ending cell address so that everything is captured for sorting purposes. The result is that your entire data set will be sorted based on the data in column E.

2. Sorting Dynamic Data in a Single Column

There may be instances wherein your starting point is defined, but your endpoint is dynamic. In such a case, you can make your code dynamic so that it picks up the end of the range automatically.

To accommodate the change in data, use the code below:

Where:

End(xlDown): This function will auto-pick the last populated cell automatically

Note: If the formula encounters a blank cell within a column, it will consider the preceding cell as the end of the range.

3. Sorting Multiple Columns Together

There might be situations when you want to sort data in multiple columns in one go. To do so, you can use the following code to achieve your purpose:

4. Sorting Columns Across Multiple Sheets

When you’re dealing with multiple sheets, you might want to get your data ready to be put into a dashboard. One of the most important aspects of data preparation is sorting it, and getting the data arranged in a certain format to present to your stakeholders or clients.

One option is to cycle through each sheet manually, sort the required columns, and then proceed to the next step. Alternatively, why not let VBA do it for you?

The intent of the code below is to cycle through each available sheet in the workbook, and based on the columns specified, sort the available data.

Here’s how you can sort columns across multiple sheets:

All the information starting with single quotes are VBA comments. These are not executed during the execution stage. However, every VBA comment you add is a meaningful addition to the code, as you can define the essence, functionality, and other relevant portions within the code sections.

5. Copying Sorted Data From One Sheet to Another

Imagine a situation where you want to sort the data and copy the whole dataset (or parts thereof) into a newly added sheet. In such cases, you can use the below code to perform the task at hand:

The above code will sort the values in column E, add a new sheet Results into the existing workbook, and paste the sorted results in cell A1.

Creating Sorting Macros in Excel

Excel VBA is a nifty language that can save you a lot of time and effort. By using VBA macros, you can create extensive dashboards, easily sort data with a few clicks, and perform different functions with ease.

Luckily, Excel’s functionality doesn’t end with macros. Check out some tips and tricks to become a more efficient Excel user.