VBA to Auto sort column when new data is added to a cell

dan4

New Member
Joined
Nov 23, 2010
Messages
34
Hello, I have a worksheet with with 10 columns of data used references in a data validation list. I want to automatically sort the column when new data is added. For example:

Column A = Inquiry Status (Closed, Open, Pending)
Column B = Source (Internal, External)
Column C = Country (Australia, Belgium, China, etc.
etc. etc.

If I add a new Country to Column C, I would like just Column C auto sorted with the header in C1 remaining in place.
If I add a new Source to Column B, I would like just Column B auto sorted with the header in B1 remaining in place.

Etc. Etc. Any help would be appreciated.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
965
Office Version
  1. 365
Platform
  1. Windows
Try to put this behind the right sheetmodule, but the rest is sorting too, based on the specific column you are changing

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Or Target.Column = 3 And Target.Count = 1 Then Cells(1, 1).CurrentRegion.Sort Cells(2, Target.Column), 1, , , , , , 1
End Sub
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
965
Office Version
  1. 365
Platform
  1. Windows
If you don't want that you can try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Column = 2 Or Target.Column = 3 And Target.Count = 1 Then
   Cells(1, 1).CurrentRegion.Columns(Target.Column).Sort Cells(2, Target.Column), 1, , , , , , 1
 End If
End Sub
 

dan4

New Member
Joined
Nov 23, 2010
Messages
34
Hello and thank you. It worked for columns 2 and 3. Can you please advise the code change to include Columns 1-10?
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
965
Office Version
  1. 365
Platform
  1. Windows
Changing the first line of code to check if the target.column is 10 or lower

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Column <= 10 And Target.Count = 1 Then
   Cells(1, 1).CurrentRegion.Columns(Target.Column).Sort Cells(2, Target.Column), 1, , , , , , 1
 End If
End Sub
 
Solution
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,331
Messages
5,836,683
Members
430,444
Latest member
WrenchBoy

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
Top