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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Well... I learned several things today. Application.Inputbox is different from Inputbox, and, if you set the variable for it as a variant, the return from the former will be a string if it's something and a boolean False if it's canceled.
So, try this:

VBA Code:
Sub inputbx()
Dim blnAns As Boolean
Dim x As Variant

x = Application.InputBox(Prompt:="Do you want filter a particular item?" & vbCr & _
      "1 = True, 0 = False", Title:="Filter List")

'take care of cancel
If (VarType(x) = vbBoolean) And (x = False) Then End

'I love Select Case
Select Case x
'Gave you some bonus correct answers... they're FEATURES!
Case 0, "No", "no", "False", "false"
     blnAns = False
Case 1, "Yes", "yes", "True", "true"
    blnAns = True
Case Else
    MsgBox "Please try again", vbCritical
    Exit Sub
End Select

MsgBox "You have responded within range allowed :" & blnAns, vbInformation

End Sub
 
Upvote 0
This is what I came up with:

VBA Code:
Sub Tests()
'// Vars
'
1    Dim blnAns As Variant
'
'// 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:=6)
'
    If blnAns = False Or blnAns = 0 Then
        MsgBox ("Cancel was pressed or zero was entered or 'False' was entered!")
        Exit Sub
    ElseIf blnAns = vbNullString Then
        MsgBox "Please try again", vbCritical
        Exit Sub
    Else
        MsgBox "You have responded within range allowed :" & blnAns, vbInformation
    End If
End Sub
 
Upvote 0
Well... I learned several things today. Application.Inputbox is different from Inputbox, and, if you set the variable for it as a variant, the return from the former will be a string if it's something and a boolean False if it's canceled.
So, try this:

VBA Code:
Sub inputbx()
Dim blnAns As Boolean
Dim x As Variant

x = Application.InputBox(Prompt:="Do you want filter a particular item?" & vbCr & _
      "1 = True, 0 = False", Title:="Filter List")

'take care of cancel
If (VarType(x) = vbBoolean) And (x = False) Then End

'I love Select Case
Select Case x
'Gave you some bonus correct answers... they're FEATURES!
Case 0, "No", "no", "False", "false"
     blnAns = False
Case 1, "Yes", "yes", "True", "true"
    blnAns = True
Case Else
    MsgBox "Please try again", vbCritical
    Exit Sub
End Select

MsgBox "You have responded within range allowed :" & blnAns, vbInformation

End Sub
Hi,

Thank you for your reply.

I have changed code with proper case and it eliminates case problems.
Revised code
VBA Code:
Sub inputbx2()
    Dim blnAns As Boolean
    Dim x As Variant
    
    '~~> Get answer as Variant
    x = Application.inputbox(Prompt:="Do you want filter a particular item?" & vbCr & _
    "1 = True, 0 = False", Title:="Filter List")
    
    '~~> Check for cancel button clicked
    If (VarType(x) = vbBoolean) And (x = False) Then
        MsgBox "Sorry, but you cannot continue", vbCritical
        Exit Sub
    End If
    
    
    '~~> Case Statements with Proper to avoid case sensitivity
    Select Case Application.Proper(x)
        '~~> Case 0 stands for False or "No" if user types it.
        Case 0, "No", "False"
            blnAns = False
         '~~> Case 1 stands for True or and "Yes" if user types it.
        Case 1, "Yes", "True"
            blnAns = True
        Case Else
            MsgBox "Please try again", vbCritical
            Exit Sub
    End Select
    
    MsgBox "You have responded within range allowed :" & blnAns, vbInformation
    
End Sub
 
Upvote 0
Solution
This is what I came up with:

VBA Code:
Sub Tests()
'// Vars
'
1    Dim blnAns As Variant
'
'// 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:=6)
'
    If blnAns = False Or blnAns = 0 Then
        MsgBox ("Cancel was pressed or zero was entered or 'False' was entered!")
        Exit Sub
    ElseIf blnAns = vbNullString Then
        MsgBox "Please try again", vbCritical
        Exit Sub
    Else
        MsgBox "You have responded within range allowed :" & blnAns, vbInformation
    End If
End Sub
The code doesn't work when I enter zero.
 
Upvote 0
It sure does. It is handled as a cancel.
It does handle cancel but if I enter number zero it failed. I have changed code to make it work.

VBA Code:
Sub Tests()
'// Vars
'
1    Dim blnAns As Variant
'
'// 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:=6)
'
    If (VarType(blnAns) = vbBoolean) And (blnAns = False) Or blnAns = vbNullString Then
    MsgBox "Sorry, but you cannot continue", vbCritical
    ElseIf blnAns = False And blnAns = 0 And blnAns = vbNullString Then
        MsgBox "Please try again", vbCritical
        Exit Sub
    Else
        MsgBox "You have responded within range allowed :" & blnAns, vbInformation
    End If
End Sub

Biz
 
Upvote 0
If you say so.

Perhaps you can explain when
VBA Code:
        MsgBox "Please try again", vbCritical
Would be executed in your code.
 
Last edited:
Upvote 0
If you say so.

Perhaps you can explain when
VBA Code:
        MsgBox "Please try again", vbCritical
Would be executed in your code.
When first input box arrives you don't enter anything and press either "Ok" or "Cancel" then executes MsgBox "Please try again", vbCritical.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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