Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch VBA error in a variable declared as string

Tags:

excel

vba

I don't understand the code below, please help.

It keeps on returning a type mismatch error against the variable 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
like image 688
Teacher Ai Avatar asked May 26 '26 07:05

Teacher Ai


1 Answers

You can use Application.InputBox to force a text input for a and and numeric input for b

Using UCASE$ shortens your 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
like image 167
brettdj Avatar answered May 30 '26 08:05

brettdj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!