VBA Sort Not Working

katk

Board Regular
Joined
Jul 21, 2009
Messages
62
Hi all,

As part of a much larger code, I have the following, which is supposed to sort a worksheet based on the values in columns F and G which are text like "CALIFORNIA" and "KINGS RIDGE". The code runs through without errors, but the data doesn't actually get sorted (any of it). At one point, I thought it was because there was a leading space (eg " CALIFORNIA") but I set up an LTrim before the sort to get rid of the spaces and it's still not working, so now I'm just confused. I don't know if there's a way to specify "A to Z" as the Sort order in VBA but I think I've sorted text using Ascending before and it's worked... Any help on figuring out why my Sort is failing would be greatly appreciated. Thanks!

Code:
Cells.Select
    ActiveWorkbook.Worksheets("Item Setup").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Item Setup").Sort.SortFields.Add Key:=Range( _
        "F1:F1216"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Item Setup").Sort.SortFields.Add Key:=Range( _
        "G1:G1216"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
     With ActiveWorkbook.Worksheets("Item Setup").Sort
        .SetRange Range("A1:J2692")
        .Header = xlYes
    End With
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I think that you are missing the line in red

Rich (BB code):
     With ActiveWorkbook.Worksheets("Item Setup").Sort
        .SetRange Range("A1:J2692")
        .Header = xlYes
        .Apply
    End With

If that doesn't fix it, try recording a macro whilst sorting manually to get the correct syntax.
 
Upvote 0
All you've done is celared/set the Sort properties, but not to actually sort.

Try adding .Apply to the end of your with statement.

You might also consider making the sort ranges dynamic.

HTH,
 
Upvote 0
Ah, thanks! That worked! That line must have gotten lost somehow while I was combining bits of code...
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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