I need help with writing a macro to select to the last row of a given range of columns. My data is in the range of A:I.
I need to sort first on column B, then sort by Column C. Here is the code I have thus far. But I cannot get it to execute without giving an error "Unable to get the Sort property of the Range class.
I need to sort first on column B, then sort by Column C. Here is the code I have thus far. But I cannot get it to execute without giving an error "Unable to get the Sort property of the Range class.
Code:
Sub test()
Dim wp As Worksheet
Set wp = Sheet5
lastrow = wp.Range("I" & Rows.Count).End(xlUp).Row
With wp
.Range("A2:I" & lastrow).Sort.SortFields.Add Key:=Range( _
"B2:B" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
.Range("A2:I" & lastrow).Sort.SortFields.Add Key:=Range( _
"C2:C" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With .Sort
.SetRange Range("A2:I" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub