Need to know if a variable can be placed in a range

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
Here is a portion of a macro I am working on:

<font face=Courier New>YesNo = MsgBox("Are you sure you want " & Z & " ?", vbYesNo)<br>    <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> YesNo<br>    <SPAN style="color:#00007F">Case</SPAN> vbNo<br>    <SPAN style="color:#00007F">GoTo</SPAN> Alpha<br>    <SPAN style="color:#00007F">Case</SPAN> vbYes<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>   </FONT>
<font face=Courier New><SPAN style="color:#00007F">Dim</SPAN> value <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Set</SPAN> rng1 = Range("C3:E6")<br>varRanges = VBA.Array("B100", "C100", "D100", "E100", "F100", "G100", "H100", "I100", "J100", "K100", "L100", "f100")<br></FONT>

I need to know if there is a way I can get into the range (VBA.Array) and place a variable that can take on the Z values instead of the the 100's? So if Z=95 the the array would read as B95, C95 etc...

Thanks,
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Case_Germany

Active Member
Joined
May 13, 2008
Messages
408
Hi,

try:

Code:
Sub Test()
    Dim varRanges As Variant
    Dim intTMP As Integer
    intTMP = 95
    varRanges = VBA.Array("B" & intTMP, "C" & intTMP, "D" & intTMP)
    MsgBox varRanges(0)
End Sub
Case_Germany
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Another way
Code:
Dim myRow As Long
myRow = 95
varRanges = Split(Join(Split("B C D E F G H I J K F"),myRow & ","),",")
 
Upvote 0

Case_Germany

Active Member
Joined
May 13, 2008
Messages
408
Hi jindon,

this is a treat - the nesting of the functions (Split, Join, Split) is a super idea. :)

But a question.

If I go through the code with F8 step by step I notice the following - Join does not consider probably the last entry.

The only way which I found, except the function to be split, is following:

Code:
varRanges = Split(Join(Split("B C D E F G H I J K F "), myRow & ","), ",")
A blank after the last value.

OK - it functions when I split the code like this and during the writing the last entry omitted.:

Code:
Dim varRanges
    Dim myRow As Long
    myRow = 95
    varRanges = Split("B C D E F G H I J K F", " ")
    varRanges = Join(varRanges, myRow & ",")
    varRanges = varRanges & myRow
    varRanges = Split(varRanges, ",")
Is there a way to put it in just one codeline?

Case_Germany
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
One line.
Code:
    varRanges = Split(Join(Split("B C D E F G H I J K F", " "), myRow & ",") & myRow, ",")
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Case

No problem.:)

Though I do think constructs like that could confuse matters somewhat.

Split, Join, Split... where will it end.

Mind you I'm not actually sure what the purpose of the array is in the OP's code.

Especially since column F appears twice.:eek:
 
Upvote 0

Forum statistics

Threads
1,191,165
Messages
5,985,039
Members
439,935
Latest member
Monty238

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