Excel VBA:具有多个条件的输入框文本

时间:2021-09-13 22:28:28

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:

我想创建一个文本框,将用户的输入存储在变量“UserChoice”中。代码如下:

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.

用户可以键入的选项是“A”,“B”和“C”。根据用户的输入,后续代码将使用If Then语句执行。

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"?

例如,如果用户输入“A,B”,是否可以同时运行If UserChoice =“A”和If UserChoice =“B”?

Thank you

1 个解决方案

#1


3  

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

首先测试A,B或C中的任何一个,然后通过测试每个代码来执行代码。

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

#1


3  

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

首先测试A,B或C中的任何一个,然后通过测试每个代码来执行代码。

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