Range name as variable: possibe?

KerryFSA

New Member
Joined
Dec 5, 2018
Messages
49
Wrote a long explanation and it didn't post, so simply put: can a range be named using a variable?
e.g. Set Rng = "Range" & N
Then Rng becomes Range1, Range2, etc in a loop.

This works to name sheets as variables ( sh = "Sheet" & M).
Doesn't seem to work with a range name. Get "type mismatch" error.

Thanks.
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Assuming those are named ranges,

Code:
Set Rng = Range("Range" & N)
 
Upvote 0
Assuming those are named ranges,
Set Rng = Range("Range" & N)

yes Range1, Range2 etc are defined with values.
But the code still hangs up on the Set Rng statement. (1004 - Method 'Range' of object'_Global' failed.)
 
Upvote 0
Dim Range1 as Range, Range2 as range, Rng as Range
Set Range1 = .Range(.Cells(a,b), .Cells(c,d))

etc.
Is this what you mean?
 
Upvote 0
Ah, those are range variables, not range names.

No; VBA lacks reflection (I think that's the relevant attribute) that would enable you to access a variable by cobbling together its name.

There are surely other ways to do whatever it is you're trying to do, but I don't know what that is.
 
Upvote 0
I'm new enough that I don't understand the difference between a range variable and a range name.

There are other ways to deal with the issue; they're just a little less elegant. Thanks very much for your help.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
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