Excel macros might not be as comprehensive as other automation tools, but the validity and efficiency of Excel VBA can’t be undermined. If you work in Excel and other Microsoft Office tools like Word and Access, you can't go wrong with VBA and its capabilities.
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:
Sub sortwithheaders()
Workbooks("Financial Sample.xlsx").Sheets(1).Activate
Range("A1:P701").sort Key1:=Range("e1"), Order1:=xlAscending, Header:=xlYes
End Sub
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:
Sub sortwithheaders()
Workbooks("Financial Sample.xlsx").Sheets(1).Activate
Range("A1", Range("A1").End(xlDown)).sort Key1:=Range("e2"), Order1:=xlAscending, Header:=xlYes
End Sub
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:
Sub SortMultipleColumns()
With Worksheets("Sheet1")
With .Cells(1, "A").CurrentRegion
.Cells.sort Key1:=.Range("B1"), Order1:=xlAscending, _
Key2:=.Range("E1"), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
End With
End Sub
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:
Sub SortWS()
Dim ws As Worksheet
'Activate the intended worksheet
Workbooks("Financial Sample.xlsx").Activate
'Cycle through each individual worksheet automatically using for loop
For Each ws In ActiveWorkbook.Sheets
'activate each individual worksheet
ws.Activate
'Define the range and then sort the column basis your requirements. In this case, only one column in being sorted.
Range("A1", Range("p1").End(xlDown)).sort Key1:=Range("E1"), Order1:=xlDescending, Header:=xlYes
'Command used to cycle to the next worksheet, once the previous worksheet has been sorted
Next ws
End Sub
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:
Sub SortWS()
Dim ws As Worksheet
'Activate the intended worksheet
Workbooks("Financial Sample.xlsx").Activate
'Cycle through each individual worksheet automatically using for loop
For Each ws In ActiveWorkbook.Sheets
'activate each individual worksheet
ws.Activate
'Define the range and then sort the column basis your requirements.
'In this case, only one column in being sorted.
Range("A1", Range("p1").End(xlDown)).sort Key1:=Range("E1"), Order1:=xlDescending, Header:=xlYes
'Command used to cycle to the next worksheet, once the previous worksheet has been sorted
Next ws
'Create a new worksheet within the workbook to store the new data
ActiveWorkbook.Sheets.Add.Name = "Results"
'Copy paste the sorted data into the newly added sheet
Sheets("Sheet1").Range("A1:p701").Copy Destination:=Sheets("Results").Range("a1")
End Sub
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.