ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dynamic variable Reference (https://www.excelbanter.com/excel-programming/306334-dynamic-variable-reference.html)

RNM

Dynamic variable Reference
 
In VBA, how does one make a reference to a dynamic veraiable?

If I have named ranges "ABC1" thru "ABC9".
User enters "5" so I want to get the value of range "ABC5".
Without using Case statements.

Thanks

--
Message posted from http://www.ExcelForum.com


Myrna Larson

Dynamic variable Reference
 

RangeName = "ABC" & Format$(n)

where n contains the number the user entered.

On Sat, 7 Aug 2004 20:15:48 -0500, RNM
wrote:

In VBA, how does one make a reference to a dynamic veraiable?

If I have named ranges "ABC1" thru "ABC9".
User enters "5" so I want to get the value of range "ABC5".
Without using Case statements.

Thanks,


---
Message posted from http://www.ExcelForum.com/



RNM[_3_]

Dynamic variable Reference
 
How about if I need to reference a variable, not a range.

user enters a number(1-9).
I need to put a value in variable either "a1", "a2",... or "a9"
without using case statement

Thank you

--
Message posted from http://www.ExcelForum.com


Amedee Van Gasse[_3_]

Dynamic variable Reference
 
RNM wrote:

How about if I need to reference a variable, not a range.

user enters a number(1-9).
I need to put a value in variable either "a1", "a2",... or "a9"
without using case statement

Thank you,


---
Message posted from http://www.ExcelForum.com/


Use an array.

Dim a(1 To 9) As Integer ' Or whatever other variable type
This single statement gives you 9 variables: a(1), a(2), a(3), ...
until a(9).

And then you use a(n) where n is the number the user entered.

--
Amedee Van Gasse using XanaNews 1.16.3.1
If it has an "X" in the name, it must be Linux?
Please don't thank me in advance. Thank me afterwards if it works or
hit me in the face if it doesn't. ;-)

I found 2 small shortcomings on www.excelforum.com:
1. You do *not* have to pay for access to news:msnews.microsoft.com
2. A *real* newsreader (not Outlook Express) is actually very easy to
use. See www.newsreaders.com for more info.


All times are GMT +1. The time now is 05:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com