VBA sorting error

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello All,

I used the macro recorder to sort a range automatically and it seemed to work when I recorded it, but now I get an error, I was hoping someone could help me figure out why

Code:
Sub range()    range("B5:V341").Select
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("B5:B341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("D5:D341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("C5:C341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Shed").Sort
        .SetRange range("B5:V341")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


End Sub

thanks,

Andrew
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
what error do you get ?
have you added / modified rows / columns ??
 
Upvote 0
Try this:
The script did not like the name you gave the script.
"Sub Range" is not a good name
After changing the name it worked for me.

Code:
Sub MY_Sort()
range("B5:V341").Select
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("B5:B341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("D5:D341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Shed").Sort.SortFields.Add Key:=range("C5:C341"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Shed").Sort
        .SetRange range("B5:V341")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,217,331
Messages
6,135,938
Members
449,973
Latest member
jarzack

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