Change Recorded Range To Used Range.

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I have this section of code that I recorded, how do I change it to the used range so it can be used on different files that have different amount of rows and columns please?

Code:
Cells.Select
    Sheets("Sheet1").Activate
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C2:C611582"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, _
        255, 0)
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add key:=Range( _
        "A2:A611582"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ611582")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You can capture the Used Range in a range object variable like this:
VBA Code:
'   Capture used range in a range object variable
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange

Then you can replace range references in your code with this variables, i.e.
this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ611582")
can be replaced with:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange rng
 
Upvote 0
You can capture the Used Range in a range object variable like this:
VBA Code:
'   Capture used range in a range object variable
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange

Then you can replace range references in your code with this variables, i.e.
this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ611582")
can be replaced with:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange rng
Thanks but what about the top bit of code where there are other ranges?
 
Upvote 0
Thanks but what about the top bit of code where there are other ranges?
They are not referencing the complete Used Range, but rather specific columns. So that would not work for those ones.

Perhaps I would suggest a different method, if that is what you want to do. Rather than find the Used Range, how about finding the last row, i.e.
VBA Code:
Dim lr as Long
lr = Range("A1").SpecialCells(xlLastCell).Row

Then, just substitute this in for your various ranges, i.e.
this:
VBA Code:
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C2:C611582"), _
becomes this:
VBA Code:
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C2:C" & lr), _

and this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ611582")
becomes this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ" & lr)

etc.
 
Upvote 0
They are not referencing the complete Used Range, but rather specific columns. So that would not work for those ones.

Perhaps I would suggest a different method, if that is what you want to do. Rather than find the Used Range, how about finding the last row, i.e.
VBA Code:
Dim lr as Long
lr = Range("A1").SpecialCells(xlLastCell).Row

Then, just substitute this in for your various ranges, i.e.
this:
VBA Code:
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C2:C611582"), _
becomes this:
VBA Code:
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("C2:C" & lr), _

and this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ611582")
becomes this:
VBA Code:
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:AZ" & lr)

etc.
But AZ may not be the last column in other files?
 
Upvote 0
Well, you could find the last used cell like this:
VBA Code:
'   Find last cell on sheet
    Dim lastCell As Range
    Set lastCell = Range("A1").SpecialCells(xlLastCell)

Then, where you are trying to reference the ENTIRE Used Range, you could do it like this:
replace this:
VBA Code:
.SetRange Range("A1:AZ611582")
with this:
VBA Code:
.SetRange Range("A1:" & lastCell.Address)

And where you want to update the last row on specific columns, you could replace references like this:
VBA Code:
Range("C2:C611582")
with this:
VBA Code:
Range("C2:C" & lastCell.Row)
 
Upvote 0
Well, you could find the last used cell like this:
VBA Code:
'   Find last cell on sheet
    Dim lastCell As Range
    Set lastCell = Range("A1").SpecialCells(xlLastCell)

Then, where you are trying to reference the ENTIRE Used Range, you could do it like this:
replace this:
VBA Code:
.SetRange Range("A1:AZ611582")
with this:
VBA Code:
.SetRange Range("A1:" & lastCell.Address)

And where you want to update the last row on specific columns, you could replace references like this:
VBA Code:
Range("C2:C611582")
with this:
VBA Code:
Range("C2:C" & lastCell.Row)
I am getting well confused now! Could you write the code as I need it please?
 
Upvote 0
Please make an effort to try to understand it. I am not a big fan of just spoonfeeding answers and have people use it, having no idea how it works. I like to help people learn, not just do their work for them. You have been here over 10 years now and have made over 3000 posts, so I think you have the ability to understand it, if you just spend a little time with it. I will break the concepts down for you.

Just like you can have variables to hold values, you can have variable to hold objects like ranges.
So all we are doing initially is created a range variable to hold the range of the last used cell on the sheet.
That is all this does:
VBA Code:
    Dim lastCell As Range
    Set lastCell = Range("A1").SpecialCells(xlLastCell)

Once you have that variable, you can reference various things through its properties, i.e.
lastCell.Address - returns the address of that last cell
lastCell.Row - returns the row number of that last cell
lastCell.Column - returns the column number (not letter) of that last cell

Once you have those, you can replace those pieces in any of your range references.
Just remember that literal values go in between double-quotes, and variables do not, and you join them with an "&".
So, if you wanted the range that starts in cell C1 and goes to the last cell in column C, it would look like:
VBA Code:
Range("C1:C" & lastCell.Row)

So this should give you all that you need to do what you want. It is all right here. Please try it out for yourself (that is how you will learn!).
If something isn't quite working out for you, or you have a specific question about one of these details, please post what you have tried here and we will help you walk through it.
 
Upvote 0
Thanks, I will just count the rows and columns and enter into the code on each different file.
 
Upvote 0
Thanks, I will just count the rows and columns and enter into the code on each different file.
That is what the code I gave you does! It will tell you the last row and column on your sheet dynamically.
No need to do anything manually.
And I showed you exactly how to incorporate those values into the code.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

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