0

I want to create a text box that will store the user's input in the variable "UserChoice". The code for this is as follows:

Dim UserChoice As String
    UserChoice= Application.InputBox("Enter Type (A, B, C):  ", "Input Box Text", Type:=2)

The options the user can type are "A", "B", and "C". Depending on the user's input, subsequent code will execute using If Then statements.

Is there a way for the user to type two or more of the options and have both of the related box of codes execute?

For example, if the user enters "A, B", is it possible to run both If UserChoice = "A" and If UserChoice = "B"?

Thank you

4
  • Of course - just include that in your Case statement or add it as another facet of your If statements. Commented Aug 16, 2018 at 17:14
  • Okay awesome, do you have an example? Commented Aug 16, 2018 at 17:15
  • Jeeped looks to have provided a solution below - using InStr to determine if the letter you're looking for is there. Commented Aug 16, 2018 at 17:18
  • @dwirony, I avoided the use of Select Case because you would have to split UserChoice and loop through each element of the resulting array. Experience has told me that you cannot rely on user input to type a static delimiter properly. Could be any of <space>, <comma-space>, <semi-colon-space> or something else. Commented Aug 16, 2018 at 17:24

1 Answer 1

3

Test for any of A, B or C first then execute code by testing for each.

Dim UserChoice As String
UserChoice= ucase(Application.InputBox("Enter Type (A, B, C):  ", "Input Box Text", Type:=2))

if not iserror(application.match(left(UserChoice, 1), array("A", "B", "C"), 0)) then
    if cbool(instr(1, UserChoice, "A", vbtextcompare)) then
        'A is found, run code
    end if
    if cbool(instr(1, UserChoice, "B", vbtextcompare)) then
        'B is found, run code
    end if
    if cbool(instr(1, UserChoice, "C", vbtextcompare)) then
        'C is found, run code
    end if
end if
Sign up to request clarification or add additional context in comments.

2 Comments

Hi there, the code works for A, B, C. However, it doesn't work if I replace them for ABC, BCD, CDE. What should I do to fix it?
It will work for ABC. It will work for the B & C of BCD. It will work for the C of CDE. You never specified that you wanted D, E or even F conditions but those should be very obvious modifications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.