By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,830 Members | 682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,830 IT Pros & Developers. It's quick & easy.

create a new variable with concatenation from the value of previous variable

P: 232
I want to create a new variable with concatenation from the value of previous variable with use of concatenation
Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2. v1 = "2"
  3. ''create new variable v12 whose value is 3
  4. v1.[&v1] = "3"
  5. Debug.Print v12
  6. '' above should print 3
  7. End Sub
error is invalid or unqualified reference
please help
Oct 14 '17 #1
Share this Question
Share on Google+
3 Replies

Seth Schrock
Expert 2.5K+
P: 2,937
Line 4 is not a valid statement. I'm not sure what you are trying to do. Simple variables (like v1) don't have any properties or methods (anything after the dot). Simply entering v12 = "3" for line 4 would get the result of Debug.Print to be 3. However, there isn't any dependence on the variable v1 using this method.

Please try to explain again what you are trying to do as you can't concatenate anything with "3" and get simply "3" other than an empty string.
Oct 24 '17 #2

Expert Mod 10K+
P: 12,357
You can't dynamically create variable names. I don't know where you pulled that syntax from but it's not VBA syntax.

If you want to store key-value pairs where you can dynamically create the key name, use a dictionary object.

However, that's only addressing your stated request. At the root of everything, you probably don't need this sort of functionality in the first place. We don't know why you want to create dynamic variable names. If we understood why you want to do this, then there's probably an easier solution and you don't need to create these dynamic variable names in the first place.
Oct 24 '17 #3

Expert Mod 15k+
P: 31,418
As Rabbit says, Arrays, Collections or Dictionary Objects seem to offer the sort of thing you look like you might be after. Without better sense in the question though it's hard to guess how to help further.
Oct 30 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.