Application InputBox Cancel/Wrong text entered problems

melewie

Board Regular
Joined
Nov 21, 2008
Messages
188
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am having some problems with application input box's, I'm trying to use them as a way to enter a password before the user is allowed access to a sheet, the code below is what I have, this works if the user enters the right password and if the user hits the cancel button.........however if the user enters the wrong password I get a Runtime 13 type mismatch on this line
Code:
If pword = False Then
which I don't understand :confused:. I must be missing something........(like a basic understanding of VBA!!)

Code:
Private Sub CommandButton1_Click()
Dim pword As String


pword = Application.InputBox("Enter password to view safety sheet", "PASSWORD REQUIRED")


If pword = "Safety" Then


    Worksheets("safety").Visible = True
    Worksheets("safety").Select
    Worksheets("Navigator").Visible = xlVeryHidden


Else
    If pword = False Then Exit Sub
    MsgBox "Password incorrect please try again"
End If


End Sub

Any help is hugely appreciated (I've got a right headache :rofl:)

Cheers

Lewie
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this:-
NB:- To have:- Pword = false , pWord would need to be a "Boolean" variable, That is, the Result would need to be "True/False"

Code:
Dim pword As String
pword = Application.InputBox("Enter password to view safety sheet", "PASSWORD REQUIRED")
If pword = "Safety" Then
    Worksheets("safety").Visible = True
    Worksheets("safety").Select
    Worksheets("Navigator").Visible = xlVeryHidden
ElseIf Not pword = "Safety" Then
    MsgBox "Password incorrect please try again"
    Exit Sub
End If
 
Last edited:
Upvote 0
Cheers for the reply Mick, Already tried that exact thing, but when the user selects cancel (cancel returns False(or so I believe)) the msgbox saying wrong password is displayed, I want to avoid this and have cancel exit the sub?? which works now, but if user enters wrong password and clicks 'OK' it errors out :crash:

I have tried setting as Boolean but this bugs out on
Code:
[COLOR=#333333]pword = Application.InputBox("Enter password to view safety sheet", "PASSWORD REQUIRED")[/COLOR]
 
Upvote 0
You could change the "pword" Variable Type to "Variant" as below, this would cover your String or Boolean
Something like:-
Code:
Dim pword As Variant
pword = Application.InputBox("Enter password to view safety sheet", "PASSWORD REQUIRED")
If pword = "Safety" Then
    Worksheets("safety").Visible = True
    Worksheets("safety").Select
    Worksheets("Navigator").Visible = xlVeryHidden
ElseIf pword = False Then
    Exit Sub
ElseIf Not pword = "Safety" Then
     MsgBox "Password incorrect please try again"
     Exit Sub
End If
 
Upvote 0
Mick I love you!!!!:eeek: completely forgot about variant. all working now cheers!!
 
Upvote 0
Microsoft still don't document how to handle errors with Application.Inputbox

If your Sub is just taking a single input, you could try something like this:

VBA Code:
Sub getRangeInput()
Dim defaultRange as Range, actualRange as Range

Set defaultRange = ActiveSheet.Range("$A$1:$A100")

On Error GoTo ErrHandler
Set actualRange = Application.InputBox(Prompt:="Enter the Range", Title:="Range Gizmotron", Default:=defaultRange.Address,  _
                                      Type:=8)

'do your stuff on the range here
Exit Sub

ErrHandler:
On error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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