Problems with cancel/accept inputbox button VBA

31 Views Asked by At

I have an inputbox that asks the user for the number of relatives they live with. But in the inputbox when I clicked on the cancel or accept button I got a type mismatch error. However, what I was able to solve was that when I click cancel or accept, I get an empty form and I simply want it to do nothing that does not execute any action.

   numFamiliares = Val(InputBox("How many family members do you live with?"))
    If numFamiliares = 0 Then Exit Sub

    familiarActual = 1

....Here goes the rest of the code....

I simply want it not to show me the empty form as it is doing, only if the user clicks accept or cancel, simply do nothing, it must execute the rest of the code only if the number of family members is entered.Link File. Here I leave the link of the file if anyone wants to see it, it was built in execution mode.

1

There are 1 best solutions below

1
Darren Bartrup-Cook On

An inputbox returns a string value so check it's not blank first, then check if it's numeric before acting on it.

Sub Test()

    Dim ans As String
    ans = InputBox("How many family members do you live with?")
    
    Dim numFamiliares As Long
    If ans <> "" Then
        If IsNumeric(ans) Then
            numFamiliares = Val(ans)
        Else
            MsgBox "Input is not numeric.", vbOKOnly + vbInformation
        End If
    Else
        MsgBox "Input is blank.", vbOKOnly + vbInformation
    End If

End Sub