Coming from Python, I cannot figure out a way to accomplish my end goal since a function like eval() does not exist with Excel VBA.
Below is what I am trying to achieve by using a simple example that I can scale:
Dim userinput as String
userinput = inputbox("A=John, B=Kate, C=Tim", "Enter a letter that corresponds with your name: ")
Dim A as String
Dim B as String
Dim C as String
A = John
B = Kate
C = Tim
Dim Phrase as Variant
phrase = msgbox("Hello, my name is " & userinput)
Result:
User enters a C
msg box pops up as "Hello, my name is Tim"
Regardless of what I try to do, I cannot have a userinput correspond to a variable that is then interpreted while in a string. My macro would be useless without a user's input referencing a much longer and complex phrase that would be too hard for a user to enter on their own.
The user is typing a letter. You need code to more explicitly translate that into a name. There are a lot of methods - if you have a long list or a user-entered list you may need a solution that is more dynamic. However here is a fix for your code:
Note: don't forget to enclose strings in double quotes.
You're presenting a finite, pre-determined set of options to the user.
Yet you let them enter quite literally anything, not validated, and roll with it.
A much better alternative would be to ditch the
InputBox
approach and present the user with an actualUserForm
, so that the user's input is constrained to a finite, pre-determined set of options - aComboBox
control comes to mind:The UserForm's code-behind is pretty straightforward - hide the form when a button is clicked, treat "X-out" as a cancellation, expose the user's
Selection
and whether the form was cancelled viaProperty Get
members, and populate the dropdown with the possible values to pick from:And now you can do this:
This handles the user cancelling out of the prompt, and if your
ComboBox
control'sStyle
property is set tofmStyleDropDownList
, there's absolutely no way the user can supply a value you're not expecting. And you can extend it further by supplyingProperty Get
andProperty Let
members to configure theTitle
andInstructions
labels as needed, or make the calling code responsible for supplying the list of valid values to populate theComboBox
with.That's one part of the problem. The next part is "mapping" inputs with another string value. The best data structure for this is a
Dictionary
, but a keyedCollection
can work just as well:Of course there are many ways to populate the data you need here, both for the combobox values and the mapped strings in the collection; you can hard-code them like this, or get them from a
Range
on a worksheet, or from a database, whatever rocks your boat.Not to take away from Scott's answer but for a potentially more dynamic approach it seems
class modules
andcallbyname
have an answer:Class module - class1
Test sub
Posted just because it's a different approach. It finds the position of the inputted letter in an array of all three, and then finds the corresponding item in an array of the names.