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

dan4

New Member
Joined
Nov 23, 2010
Messages
38
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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
 
Upvote 0
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
 
Upvote 0
Hello and thank you. It worked for columns 2 and 3. Can you please advise the code change to include Columns 1-10?
 
Upvote 0
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
 
Upvote 0
Solution

Forum statistics

Threads
1,211,983
Messages
6,105,208
Members
447,955
Latest member
BWheezy

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