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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
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.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

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

kind regards,
Erik
 

Forum statistics

Threads
1,136,267
Messages
5,674,729
Members
419,523
Latest member
Urnovio

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
Top