Sort Code & set Data range with variables

mt

Board Regular
Joined
Feb 24, 2006
Messages
134
Is there a way to set the data range for a sort field range using a variable to find the last column and last row? I can set the variable for the last row, but when I try to use the cell property to set the range using variables for both last row and last column, the sort does not work. I have similar problems with other functions like setting print ranges.
Thanks,

Mike

Code:
Sub Sort_Report1()
    Dim rRow As Long
    Dim nCol As Integer
    
 With Worksheets("Report1")
    rRow = .Range("A" & Rows.Count).End(xlUp).Row    'Finds last data row
    nCol = .Cells(1, 255).End(xlToLeft).Column
    
 If rRow > 1 Then
    'Sort Key:  Date,Task Code
    Worksheets("Report1").Range("A1:H" & rRow).Sort Key1:=Range("G1"), Key2:=Range("H1"), Order2:=xlAscending, Key3:=Range("A1"), Order1:=xlAscending, Header:=xlYes
    
End If
End With
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Mike

How is the code not working?

By the way I don't see where in the posted code you are using variables for row and column.
 
Upvote 0
Hi,

did you try
Code:
Range(Cells(1, 1), Cells(rRow, nCol))
instead of Range("A1:H" & rRow) ?

kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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