Skip to main content

How to auto sort column by value in Excel?

For example, you have a purchase table as below screenshot shown. Now you want the Price column to be sorted automatically when you enter new numbers/prices in this column, how could you solve it? Here I introduce a VBA macro to help you auto sort a specific column by value in Excel.

Auto sort column by value with VBA


Auto sort column by value with VBA

This VBA macro will sort all data in a specific column automatically as soon as you enter new data or change value in the column in Excel.

1. Right click current sheet name in the Sheet Tab bar, and then click the View Code from the right-clicking menu.

2. In the opening Microsoft Visual Basic for Application dialog box, paste the following VBA macro code into the opening window.

VBA: Auto Sort Column in Excel

Private Sub Worksheet_Change(ByVal Target As Range)
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

Note:
1) In the above vba code, B:B means it will auto sort Column B, B1 is the first cell in Column B, B2 is the second cell in Column B, and you can change them based on your needs.
2) The snippet Header:=xlYes in the 5th row tells Excel that the range you will sort has a header, so that the first row of the range will not be included when sorting. If there is no header, please change it to Header:=xlNo; and change Key1:=Range("B2") in the 4th row to Key1:=Range("B1").

3. Then go back to the worksheet, when you enter a new number in the Price column or modify any existing prices, the Price column will be automatically sorted in ascending order.

Note: When you enter a new number in the Price column, you must enter the number in the first blank cell below the original numbers. If there are blank cells between the new entered number and original numbers as well as blank cells between original numbers, this column won't be sorted automatically.


Demo: Auto sort column by value with VBA in Excel


Kutools for Excel: Over 300 handy tools at your fingertips! Start your 30-day free trial with no feature limitations today. Download Now!

Easily sort by frequency of occurrences in Excel

Kutools for Excel's Advanced Sort utility supports sorting data by text length, last name, absolute value, frequency, etc. in Excel quickly.


ad sort by frequency 2

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (37)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
such a good information thanks a lot
This comment was minimized by the moderator on the site
Maravilhoso! Muito obrigada pela informação, amigo!
Rated 5 out of 5
This comment was minimized by the moderator on the site
I love this, but I'd like to know what I should do if I want it to be multiple different ranges in the same column. When I use the code above, it works for the first table that I have but the bottom two tables don't sort automatically. I tried changing the range, I also duplicated the code and changed the code to match the tables, but nothing is working.

For example:
Range("L8").Sort Key1:=Range("L37"),Range("L41").Sort Key1:=Range("L62") _

Or just duplicating the code like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L8").Sort Key1:=Range("L37"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L41").Sort Key1:=Range("L62"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L66").Sort Key1:=Range("L100"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

But then it will tell me that "Ambiguos name detected: Worksheet_Change" but it won't do that if I only have the code one. Would anyone be able to help me out?
Thank you!
This comment was minimized by the moderator on the site
Hi there,

You can use the vba below:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("K:K")) Is Nothing Then
        Range("K32:K36").Sort Key1:=Range("K32"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
        
        Range("K38:K42").Sort Key1:=Range("K38"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom        

        Range("K44:K46").Sort Key1:=Range("K44"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
End Sub


Amanda
This comment was minimized by the moderator on the site
01. I have sorted on Name data in Excel Worksheet as Sheet1.
02. I want the Names that are repeated in New Worksheet as Sheet2.
This comment was minimized by the moderator on the site
Hallo
Ich habe folgenden Code, aber die Sortierung klappt leider nicht.
evtl nur eine kleine Anpassung, aber ich verzweifle hier seit Tagen.
Danke im voraus.


Sub Eindeutige_Daten()

Dim rng As Range
Dim InputRng As Range, OutRng As Range

Set dt = CreateObject("Scripting.Dictionary")

xTitleId = "Eindeutige Daten"

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)

For Each rng In InputRng
If rng.Value <> "" Then
dt(rng.Value) = ""
End If
Next

OutRng.Range("A1").Resize(dt.Count) = Application.WorksheetFunction.Transpose(dt.Keys)

'** Zelladresse in Spalten- und Zeilenangabe trennen
Dim wert() As String
wert = Split(OutRng.Address, "$")

letztezeile = ActiveSheet.Cells(1048576, wert(1)).End(xlUp).Row

With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Calculate

End Sub
This comment was minimized by the moderator on the site
Hi there,

Please debug the below snipt of your code and see if there is a problem.
With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Remember to check the values you entered.
If there is still questions, please don't hesitate to ask me.

Amanda
This comment was minimized by the moderator on the site
Love it. Works for me.

But when I repeat the exact same steps at another Excel file, and enter a number in the colomn for it to autosort, Excel closes.
This comment was minimized by the moderator on the site
Hi,
This is extremely useful.
Is there a way to expand the formula to cover multiple columns? For example, to sort data based on values on first, column B, and then column C?I would really appreciate any solutions!
This comment was minimized by the moderator on the site
Amazing thanks!!!
This comment was minimized by the moderator on the site
So this seems to work when the data is manually entered but doesn't work when it is a table that repopulates from another file....is there any way to do that?
This comment was minimized by the moderator on the site
Hi, it arrange the entire row but I have some link on cells into specific folder which is left behind after sort.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations