A collection would be another storage mechanism in VBA that might meet your need.
~bp
Main Topics
Browse All TopicsI'm not even sure if this is possible, but can you use one variable to store the name of another and evaluate based on it's value?
I know the Evaluate function is useful for thing in a worksheet, such as named ranges and such, but what about plain old variables in the VBA itself.
I can obviously get around this by setting up a string to conditionally contain the value I want to reference, or setting up a conditional statement to grab the correct variable, but it would be a heck of a lot easier on me and the code if there was a way to reference a variable by another variable and get the value of the variable being referenced.
Hope that was clear.
WC
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Basically, I have global constant strings that contain the names of output files from SAS programs. Depending on the model that was run, these files may be named differently (not my doing). Thus, I need to detect which model was run via user input, and open the appropriate file.
E.g.
2 constants
Public Constant LOREAN_MODEL_FILE_NAME As String = "summary_stats"
Public Constant SCOLORE_MODEL_FILE_NAME As String = "model_statistics"
In the code I can detect which model was selected (LOREAN or SCOLORE). I would have liked to concatenate the model type with the rest of the variable name for the file, and been able to use that to get the name of the file I need.
I was thinking of doing one of two things:
1. create some more global constants to store the values of the file names for the currently selected model
2. Insert conditional statements to set the appropriate file name in the modules that open the files.
1. requires more global variables, which essentially are copies of others, while 2. requires redundant code in multiple locations. I'm leaning towards 1.
I would be interested in hearing how a collection might be used for this purpose. As for your array idea, I've never tried storing constants in an array. I suppose they don't have to be declared on constants. The other thing is I'd have to come up with unique identifiers or else I'd have to loop through the array to search for the model - file combination.
Let me know if you have any ideas, otherwise, I'll probably just close this with your first solution accepted as the answer, since it isn't possible to do what I have asked.
WC
Depending on your motivation, you might be able to use the undocumented VarPtr, StrPtr and ObjPtr functions in VBA. These are documented at http://vb.mvps.org/tips/va
A different approach, using completely documented VBA stuff, is to set up an enum array. You can address the elements of this array by either their name or index number.
Brad
Brad,
I've used StrPtr in the past in conjunction with an InputBox in order to determine whether the user hit Cancel or pressed Ok without entering anything, thus detecting the difference between empty string and vbCancel.
Seeing as my early programming was in C++ it's nice to know that you can use pointers in VB, so thank you for the links. I'm having a little trouble implementing this though. It keeps crashing Excel.
I have tried using the Declare statement from one of the links you provided, which in turn provided another link.
Public Declare Sub CopyMemoryRead Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, _
ByVal Source As Long, ByVal Length As Long)
I then put the following in one of my macros.
Dim fileName As String
Declarations.CopyMemoryRea
TempType stores the model type (LOREAN, SCOLORE, etc.)
It's my understanding that the Declare Sub will store the value passed by the second argument into the memory location of the first, thus it should store the value of my global constant string into fileName, correct?
WC
Ok, so I figured I need to be writing to the variable fileName, not reading from it, so I am using the other declare statement now,
Public Declare Sub CopyMemoryWrite Lib "kernel32" Alias _
"RtlMoveMemory" (ByVal Destination As Long, Source As Any, _
ByVal Length As Long)
but Excel is still crashing on the line
Declarations.CopyMemoryWri
The following has returned put the string "ModelStatistics" into fileName on occasion, but more times than not I just get a string of ?, "?????????????". It also crashes still sometimes, and I cannot change the variable either.
Declarations.CopyMemoryWri
Also, this is not evaluating to the value stored in SCOLORE_MODEL_FILE_NAME, but just the string "SCOLORE_MODEL_FILE_NAME".
What I mean is that,
StrPtr(SCOLORE_MODEL_FILE_
and
StrPtr(TempType & "_MODEL_FILE_NAME")
return 2 different memory addresses when TempType is "SCOLORE".
I found the following link, but I'm unable to get the function mentioned in it to work either.
http://www.vbforums.com/sh
Really strange what's going on. I can't figure it out.
I just wanted to see if it would work if I hardcoded the variable names in. It still is pretty sketchy.
Declarations.CopyMemoryWri
This cycles through 4 different results. The first two times I execute this line of code, it returns "ModelStatistics", which isn't correct, it should be "summary_stats".
The third time it returns "???????????????" and the fourth, "?????????????????"
It then cycles back to the first one.
Ok, I changed the first argument,
Declarations.CopyMemoryWri
which is now a VarPtr. This returned "summary_stats", HELL YEA!! Unfortunately, running this a second time crashed Excel. Also, it doesn't solve the problem of how do I get this to work with the variable string that stores which model type.
Declarations.CopyMemoryWri
Obviously that Len() will not work the way I want. It returns the length of that concatenated string, not the string contained in that global constant.
It seems the function from that link I found is returning the correct length of string, but for some reason it's not returning the right string. I only get a bunch of ASCII characters, such as ? and boxes.
"?"
Public Function GetStringFromPointer(ByVal
Dim lngLength As Long, strBuffer As String
lngLength = lstrlen(lpString) 'get length of string (in chars)
strBuffer = Space$(lngLength) 'make a buffer to copy to
CopyMemoryWrite StrPtr(strBuffer), lpString, lngLength * 2 'copy the string (note that this is a unicode string, so really there are 2 bytes per char
GetStringFromPointer = strBuffer 'return the copied string
End Function
It would be great if this function worked, as I wouldn't need to create any other variables anywhere in the code, I could just call this where I needed the conditional filenames to show up.
I'm leaning back to the easiest thing to do is just set global copies when the model type is chosen....less of a headache, although this would be cool if it worked.
WC
Business Accounts
Answer for Membership
by: billprewPosted on 2009-10-12 at 10:29:09ID: 25553131
I don't believe Excel supports exactly what you are after. What you could do though is create an array, or dictionary that had key, value pairs, and then lookup a key value to get it's associated value data. It's not exactly what you asked, but depending on what you are doing may work as good or better.
~bp