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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,216,115
Messages
6,128,915
Members
449,478
Latest member
Davenil

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