在声明为string的variables中键入不匹配的VBA错误
我不明白下面的代码,请帮助。
它继续返回一个types不匹配错误对variablesb。
Dim a As Integer Dim b As String a = InputBox("Input the number of items", "Total Number of Items.") b = InputBox("Which Block?", "Total Number of Items.") Do While b <> "a" Or "A" Or "B" Or "b" MsgBox ("Invalid Block. Try again, Input A or B") b = InputBox("Which Block?", "SELECT BLOCK.") Loop If b = "a" Or "A" Then Me.ComboBox1.List = Worksheets("Sheet1").Range("a3:a39").Value Else Me.ComboBox2.List = Worksheets("Sheet2").Range("a3:a35").Value End If
我认为这是一个逻辑上的缺陷。 更改
Do While UCase$(b) <> "A" or UCase$(b) <> "B"
INTO
Do While UCase$(b) <> "A" AND UCase$(b) <> "B"
您可以使用Application.InputBox
强制为b
inputa
和和数字
使用UCASE$
缩短您的testing
Dim a As Long Dim b As String a = Application.InputBox("Input the number of items", "Total Number of Items.", , , , , , 1) b = Application.InputBox("Which Block?", "Total Number of Items.", , , , , 2) Do While UCase$(b) <> "A" And UCase$(b) <> "B" b = Application.InputBox("Which Block?", "Invalid Entry - Total Number of Items.", , , , , 2) Loop
问题是如何testing“b”variables。
错误
Do While b <> "a" Or "A" Or "B" Or "b"
对
Do While b <> "a" Or b <> "A" Or b <> "B" Or b <> "b"
if条款也是如此
是的
If b = "a" Or "A" Then
使用
If b = "a" Or b = "A" Then
编辑:
当您使用Do Loop的正确语法时,由于您如何构buildWhile条件,您的代码进入了无限循环。 例如,如果input框检索“B”,则它是“<> b”或“<> a”或…,因此它将再次进入循环。 无论用户input什么,它总会与其他任何条件不同。
编辑2:
请试试这个。 Doe它检索相同的错误?
Sub TryThis() Dim a As Integer Dim b As String a = InputBox("Input the number of items", "Total Number of Items.") b = InputBox("Which Block?", "Total Number of Items.") Do While b <> "a" And b <> "A" And b <> "B" And b <> "b" MsgBox ("Invalid Block. Try again, Input A or B") b = InputBox("Which Block?", "SELECT BLOCK.") Loop Debug.Print "InputBox b Correctly filled" If b = "a" Or b = "A" Then Debug.Print "User Input A" Else Debug.Print "User Input: " & b End If End Sub