How to Sort Smallest to Largest with VBA Autofilter in Excel: 3 Suitable Ways

Method 1 – Sort Smallest to Largest Using the Ascending Order in VBA Autofilter

Step 1:

  • Open a Module. From your Developer tab, go to Developer → Visual Basic.

  • After clicking on the Visual Basic ribbon, a window named Microsoft Visual Basic for Applications – Sort Data Smallest to Largest will instantly appear. From that window, we will insert a module for applying our VBA code. Go to Insert → Module.

Sort Smallest to Largest Using the Ascending Order in VBA Autofilter

Step 2:

  • The Sort Data Smallest to Largest module pops up. In the Sort Data Smallest to Largest module, write down the below VBA code:
Sub Autofilter_Sort_Smallest_to_Largest()
With ActiveSheet.Sort
.SortFields.Add Key:=Range("D4"), Order:=xlAscending
.SetRange Range("B4:E16")
.Header = xlYes
.Apply
End With
End Sub

  • Run the VBA code. Go to Run → Run Sub/UserForm.

Sort Smallest to Largest Using the Ascending Order in VBA Autofilter

  • After running the VBA code, you will be able to sort data from smallest to largest which has been given in the below screenshot.

Sort Smallest to Largest Using the Ascending Order in VBA Autofilter


Method 2 – Use Multiple Columns to Sort Smallest to Largest with Header in Excel VBA

Step 1:

  • Insert a new module and type the below VBA code to change the font color. The VBA code is,
Sub Sort_Multiple_Columns()
With ActiveSheet.Sort
.SortFields.Add Key:=Range("B4"), Order:=xlAscending
.SortFields.Add Key:=Range("D4"), Order:=xlAscending
.SetRange Range("B4:E16")
.Header = xlYes
.Apply
End With
End Sub

Use Multiple Columns to Sort Smallest to Largest with Header in Excel VBA

  • Run the VBA code. Go to Run → Run Sub/UserForm.

Step 2:

  • After running the VBA code, you can sort our data according to the sales representatives’ names and order products from smallest to largest, as shown in the screenshot below.

Use Multiple Columns to Sort Smallest to Largest with Header in Excel VBA


Method 3 – Apply VBA Autofilter to Sort Smallest to Largest with Worksheet Name

Step 1:

  • Insert a new module according to method 1 and type the below VBA code to change the font color. The VBA code is,
Sub Sort_Smallest_to_Largest()
Rows("4:16").Select
ActiveWorkbook.Worksheets("Smallest to Largest").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Smallest to Largest").Sort.SortFields.Add Key:=Range("E4:E16") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Smallest to Largest").Sort
.SetRange Range("B4:E16")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Apply VBA Autofilter to Sort Smallest to Largest with Worksheet Name

  • Run the VBA code. Go to Run → Run Sub/UserForm.

Step 2:

  • After running the VBA code, you will be able to sort the data from smallest to largest according to the worksheet name given in the screenshot below.

Apply VBA Autofilter to Sort Smallest to Largest with Worksheet Name


Things to Remember

You can pop up the Microsoft Visual Basic for Applications window by pressing Alt + F11 simultaneously.

If a Developer tab is not visible in your ribbon, you can make it visible. To do that, go to, File → Option → Customize Ribbon.


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdur Rahim Rasel
Md. Abdur Rahim Rasel

MD. ABDUR RAHIM is a marine engineer proficient in Excel and passionate about programming with VBA. He views programming as an efficient means to save time while managing data, handling files, and engaging with the internet. His interests extend to Rhino3D, Maxsurf C++, AutoCAD, Deep Neural Networks, and Machine Learning, reflecting his versatile skill set. He earned a B.Sc in Naval Architecture & Marine Engineering from BUET, and now he has become a content developer, creating technical content... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo