Sort range in ascending order

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Can someone show me how to sort a specific range into ascending order using VBA?

My range is A4:D29 - column A is the range I need sorting. I need rows A1:A3 left unchanged.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If you turn on the Macro Recorder and record yourself performing the steps you want it to do manually, it should give you the VBA code that you are looking for.
 
Upvote 0
Maybe...

Code:
Sub sortit()
Range("A4:D29").Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlNo
End Sub

or

Code:
Sub sortit()
Range("A4:D29").Sort Range("A4"), 1, , , , , , 2
End Sub

or just
Code:
Sub sortit()
Range("A4:D29").Sort Range("A4"), 1
End Sub
 
Last edited:
Upvote 0
I've tried that - as soon as I click custom sort it selects the whole of column A - I can't work out how to select the range from A4.
 
Upvote 0
Guys - thanks to both, MARK858 - your first suggestion does the trick.

Thanks.
 
Upvote 0
You're welcome (but all 3 codes should do the same).

Btw the macro recorder gives me

Code:
    Range("A4:D29").Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("A4:A29") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A4:D29")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Except I changed xlguess to xlno
 
Last edited:
Upvote 0
Except I changed xlguess to xlno
Well, that is rather annoying!
When you do it manually, there is a checkbox that says "My Data Has Headers".
You would think that if you left it blank, it would choose "xlNo" instead of "xlGuess"!
 
Upvote 0

Well, that is rather annoying!
When you do it manually, there is a checkbox that says "My Data Has Headers".
You would think that if you left it blank, it would choose "xlNo" instead of "xlGuess"!


I am afraid it has always done the same since the updates in Excel 2007 :(

Personally if coding (unless I need any of the newer options), I still prefer to use the old pre 2007 syntax where the default for headers is xlno (I can't remember what the recorder gave you in 2003 and earlier).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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