How to Auto-Sort Columns by Value in Excel?


When we are working with the data related to numerals, it is very important that we always sort the data in order to analyse it better. Sorting can be done in either ascending or descending order. Sorting can help us compare the values and their importance on the sheet.

Generally, in Excel, we will sort the data by using the sort function, but if we insert or update a new value in the list, then we need to do the process again. Read this tutorial to learn how you can autosort columns by value in Excel.

AutoSort Columns by Value in Excel

Here we insert VBA code into the sheet to complete our task. Let us see a simple process to understand how we can always sort columns by value in Excel using the vba application.

Step 1

Assume we have an Excel sheet with data that is similar to the data shown in the image below.

Now, to open the VBA application, right-click on the sheet name and select View Code, then type the programme into the text box, as shown in the below image.

Example

Private Sub Worksheet_Change(ByVal Target As Range) 'Updated By Nirmal On Error Resume Next If Not Intersect(Target, Range("B:B")) Is Nothing Then Range("B1").Sort Key1:=Range("B2"), _ Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom End If End Sub

In the code, "B:B" describes that all the cells under the "B" column are sorted, and "B2" is the first cell to start the sorting.

Step 2

Now save the sheet as a macro-enabled table, and in the sheet, whenever we update or insert a new value, it will be sorted in ascending order.

Note − The VBA code will not affect the existing data; it only deals with the updated or newly inserted values.

Conclusion

In this tutorial, we used a simple example to demonstrate how you can auto-sort columns by value in Excel.

Updated on: 11-Jan-2023

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements