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.
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, _
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.
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.
Full Feature Free Trial
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by
- Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- More than
300 powerful features. Supports Office/Excel
2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features
30-day free trial. 60-day money back guarantee.
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!