VBA code to sort rows in alphabetical order

wrecclesham

Board Regular
Joined
Jul 24, 2019
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Could someone please help me to create a small VBA script that is able to automatically sort the 10 rows contained within range A1:C10 in ascending alphabetical order, using the contents of cells A1:A10?

Also, if I record a macro while manually performing the above sort just using the Sort button, should this work in theory and would it produce clean VBA code, or is it sometimes better to manually write the code, instead of using the Record Macro feature? I've always wondered about that.

Here's how my test worksheet looks:

 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Also, if I record a macro while manually performing the above sort just using the sort button, should this work in theory and would it produce clean VBA code, or is it sometimes better to manually write VBA code, instead of using the record feature? I've always wondered about that.
In this case, since it is a defined range (and not a dynamically changing one), recording a macro would work fine.
 
Upvote 0
Great. Thanks.

How can I make it so that it runs whenever any values in the worksheet are updated?

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1:C10").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A1:A10") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A10")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited:
Upvote 0
I ended up using the below code instead.

It works great and is shorter than the VBA I got when I recorded the macro. :)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Range("A1:C10").Sort Key1:=Range("A1"), _
      Order1:=xlAscending, Header:=xlNo, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

End Sub
 
Upvote 0
It works great and is shorter than the VBA I got when I recorded the macro.
Excellent!

Yes, recorded code can often be cleaned up to make it a little shorter/more efficient, thought it would still work the other way too.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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