StuLux
Well-known Member
- Joined
- Sep 14, 2005
- Messages
- 682
- Office Version
- 365
- Platform
- 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