With the code below I get an error:
Run-time error '1004' Method 'Range' of object'_Worksheet failed.
Dim destLastCol As Integer 'last column in range
Dim destLastRow As Integer 'last row in range
Dim wsCrewDetail As Worksheet '
Set wsCrewDetail = Worksheets("CrewDetail_M")
destLastCol = integer assigned previously
destLastRow = integer assigned previously
With wsCrewDetail.Range(Cells(4, 1), Cells(destLastRow, destLastCol)) <== error here
.Sort Key1:=.Cells(4, 2), Order1:=xlAscending, _
key2:=.Cells(4, 1), Order2:=xlAscending, _
key3:=.Cells(4, 3), order3:=xlAscending, Header:=xlYes
End With
I’ve searched and viewed many examples trying many variations on setting the Range reference and nothing is working.
What is correct reference syntax please?
Edited to add destLastRow = integer assigned previously and edit to show destLastCol
Well you aren’t feeding a row number into
destLastRowin this lineWith wsCrewDetail.Range(Cells(4, 1), Cells(destLastRow, destLastCol))Plus, don’t use
Integervariables, useLong. They are more efficient and also cater for large numbers.suggestions
wsfor Worksheet makes it easier to type and read codeLongrather thanIntegerrng1) rather than use a longer object starting with the sheetdestLastRowanddestLastColvaluessample code