assigning a cell reference to a variable

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am looking to create a dynamic range that I can manipulate in VBA for sorting purposes. I have the following code:
VBA Code:
    Dim x As Range
    
    x = Sheet1.Range("A2").End(xlDown)
    
    Sheet1.Range("A2:" & x).Select

I would then put my sorting code after the selection. How can I make the above work where x is the last cell with a value in it from A2 down.
 

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.
Try
VBA Code:
Sheet1.Range("A2" , x).Select
 
Upvote 0
Try
VBA Code:
Sheet1.Range("A2" , x).Select
Hi Fluff, I had meant that my

x=Sheet1.Range("A2").End(xlDown) didn't work

but I managed to solve my issue through the use of the named ranges and the Offset function in the formulae.
 
Upvote 0
Missed that, it needs to be
VBA Code:
Set x = Sheet1.Range("A2").End(xlDown)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,586
Messages
6,125,683
Members
449,249
Latest member
ExcelMA

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