我不明白下面的代码,请帮忙。
它继续针对变量b返回类型不匹配错误。
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
答案 0 :(得分:5)
您可以使用Application.InputBox
强制a
的文字输入和b
的数字输入
使用UCASE$
缩短测试时间
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
答案 1 :(得分:1)
我认为这是一个合乎逻辑的缺陷。变化
Do While UCase$(b) <> "A" or UCase$(b) <> "B"
INTO
Do While UCase$(b) <> "A" AND UCase$(b) <> "B"
答案 2 :(得分:0)
问题是如何测试“b”变量。
错误
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循环的正确语法时,由于您构建While条件的方式,您的代码进入了无限循环。 例如,如果inputBox检索“B”,则它是“&lt;&gt; b”或“&lt;&gt; a”或......因此它将再次进入循环。无论用户输入什么,它总是与任何其他条件不同。
编辑2:
请试试这个。它会检索相同的错误吗?
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