VBA Auto Sort A-Z

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello this code sorts B automatically from B2 downwards when a change is made.

How would I alter the code so that it only sorts B3 and down, so B1 the title of the column and B2 are constant. Tried a few alterations to the below without any success.

Thanks.

Code:
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, _
Orientation:=xlTopToBottom


End If


End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Change this line:

Code:
Range("B1").Sort Key1:=Range("B2"), _
to

Code:
Range("B[COLOR=#ff0000]2[/COLOR]").Sort Key1:=Range("B[COLOR=#ff0000]3[/COLOR]"), _
 
Upvote 0
Change this line:

Code:
Range("B1").Sort Key1:=Range("B2"), _
to

Code:
Range("B[COLOR=#ff0000]2[/COLOR]").Sort Key1:=Range("B[COLOR=#ff0000]3[/COLOR]"), _


Thanks Eric W.

Doesn't quite work, it is still sorting B2:B

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


On Error Resume Next
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Range("B2").Sort Key1:=Range("B3"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom


End If


End Sub
 
Upvote 0
Sorry, I should have tested better. Try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        Range("B3:B1000").Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlNo
    End If

End Sub
I don't know if you need any of the other miscellaneous parameters on the Sort, but they are usually not needed.
 
Upvote 0
Sorry, I should have tested better. Try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        Range("B3:B1000").Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlNo
    End If

End Sub
I don't know if you need any of the other miscellaneous parameters on the Sort, but they are usually not needed.

Thank you, this works nicely.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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