Run time error when trying to apply a sort using VBA

StuLux

Well-known Member
Joined
Sep 14, 2005
Messages
679
Office Version
  1. 365
Platform
  1. Windows
I'm getting error 1004 when I try to apply the following code to sort a range, I've tried several different ways of defining the range (which I need to do using variables) but it keeps failing on the .Apply line of code:

Code:
    Sheets(SheetName).Select    
    SheetRecordCount = Application.WorksheetFunction.CountA("D:D")
    Sheets(SheetName).Sort.SortFields.Clear
    Sheets(SheetName).Sort.SortFields.Add2 Key:=Range("A2:H" & SheetRecordCount), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveSheet.Sort
        .SetRange Range("A2:H" & SheetRecordCount)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I believe your issue is with the part in red here:
Code:
Sheets(SheetName).Sort.SortFields.Add2 Key:=Range([COLOR=#ff0000]"A2:H"[/COLOR] & SheetRecordCount), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
You are supposed to tell it what column you are sorting by (not the entire range you want to sort), so it should be a single column range, i.e. if you want to sort by column D, then the part in red should look like "D2:D".
 
Upvote 0
I think..problem is there in Add2..it should be add

Code:
Sheets(SheetName).Sort.SortFields.Add2 Key:=Range("A2:H" & SheetRecordCount), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
 
Upvote 0
I think..problem is there in Add2..it should be add
No, that part is fine. When I record a macro to do sorting, I get the "Add2" also.
The problem is with the range he used. He needs to tell it what column he wants to sort by; it needs to be a single column reference, not a multiple column reference.
 
Upvote 0
Thanks to all of you for contributing, Joe4 you are quite right it was that first reference that was wrong and, as you say I should refer to a column not the whole range.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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