Need VBA to sort column after entering data

David Schaleger

Board Regular
Joined
Dec 6, 2010
Messages
55
I need VBA code for Excel 2007 to sort column A (includes header row) in a formated table after I enter data into any cell in column A.

This will be used in a few tables within this workbook, the tables being products, vendors, and payment types. I pull the data from these tables into a purchases table. I have to remember to sort the colums A to Z each time I enter new data, otherwise when I Alt+DownArrow in the purchases table to bring up the corresponding list, it's only alphabetized since the list time I remembered to sort A to Z. Would really like to automate this A to Z sorting procedure.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Application.EnableEvents = False
    Me.UsedRange.Sort Key1:=Range("A2"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Oh, that is SWEET! Thank you! Thank you!! Thank you!!!
Could you please add one more feature? I'd like the active cell after it sorts to be the cell in column B where the code sorts the new item to that I just added. So, if I added the new item at A50 (end of table), and it sorts this new item to A25, I'd like the active cell to be B25 so I can start entering the other relavent data without having to first find where the new item was sorted to.
 
Upvote 0
If the entries in column B are unique then perhaps

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Found As Range, x As Variant
If Target.Column = 1 Then
    Application.EnableEvents = False
    x = Target.Offset(, 1).Value
    Me.UsedRange.Sort Key1:=Range("A2"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Set Found = Columns("B").Find(what:=x, LookIn:=xlValues, lookat:=xlWhole)
    If Not Found Is Nothing Then Found.Select
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top