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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,214,422
Messages
6,119,396
Members
448,891
Latest member
tpierce

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