Sort Data in an Excel worksheet using Macro VBA (Ascending or Descending)



Sometime, we want to auto sort data manually by running the macro or by triggering by editing a row:

The following VBA code is to sort the data in Ascending in Excel Worksheet. This code will sort the data in Range A3 to C20 based on the Third Column i.e.; C3. And in Ascending order.

Sub sb_VBA_Sort_Data_Ascending()
Range("A3:C20").Sort _
Key1:=Range("C3"), Order1:=xlAscending
End Sub

The following VBA code is to sort the data in Descending in Excel Worksheet. This code will sort the data in Range A3 to C20 based on the Third Column i.e.; C3. And in Descending order.

Sub sb_VBA_Sort_Data_Descending()
Range("A3:C20").Sort _
Key1:=Range("C3"), Order1:=xlDescending
End Sub

Please follow the below instructions to execute the VBA code to sort the excel file.
Step 1: Open any existing Excel workbook
Step 2: Enter some data in A3 to C20
Step 3: Press Alt+F11 – This will open the VBA Editor
Step 4: Insert a code module from then insert menu
Step 5: Copy the above code to sort the data in excel and paste in the code module which have inserted in the above step
Step 6: Now press F5 to execute the code.

Now you can observe that the Data in Excel sheet is sorted in Ascending/Descending order (as per your requirement and the code used) based on the Column C.

No comments:

Post a Comment

Thank You.

https://linwintech.blogspot.com/