Excel InputBox

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,
I have boolean input box as I want code to only allow 0,1, True, False input only.
If nothing entered then Msgbox "Please try again" should appear.


VBA Code:
Sub Tests()
     '// Vars
1    Dim blnAns As Boolean
      
      '// Boolean prompt, 1 = true, 0 = false
2    blnAns = Application.inputbox(Prompt:="Do you want filter a particular item?" & vbCr & _
      "1 = True, 0 = False", Title:="Filter List", Type:=4)

   If StrPtr(blnAns) = 0 Then
   MsgBox ("Cancel pressed!")
    Exit Sub
   End If
      '~~> Check only 1 / True 0 / False as between entered
      If Application.Sum((blnAns >= 0 And blnAns <= 1), (blnAns = "True" Or blnAns = "False")) > 0 Then
              MsgBox "You have responded within range allowed :" & blnAns, vbInformation
          Else
              MsgBox "Please try again", vbCritical
              Exit Sub
          End If
End Sub

Your help would be greatly appreciated.

Biz
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Johnny.

I managed to make code below work. Thank you all for your help.

VBA Code:
Sub Tests_Works_Test()
1     Dim blnAns As Variant
      
      '~~> Get User Input
2     blnAns = Application.inputbox(Prompt:="Do you want filter a particular item?" & vbCr & _
       "1 = True, 0 = False", Title:="Filter List", Type:=6)
      
      '~~> The checks if input is blank with "Cancel" or "Ok" is clicked.
3     If (VarType(blnAns) = vbBoolean) Or blnAns = vbNullString Then
4         MsgBox "Sorry, but you cannot continue", vbCritical
          '~~> Checks if inputs are within allowed range.
5     ElseIf Not Application.Sum((blnAns >= 0 And blnAns <= 1), (blnAns = "True" Or blnAns = "False")) > 0 Then
6         MsgBox "Please try again", vbCritical
7         Exit Sub
8     Else
9         MsgBox "You have responded within range allowed :" & blnAns, vbInformation
10    End If
End Sub

Kind Regards

Biz
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top