Produce an error message with if statement...?

I

ianripping

I have this code: -

Sub Unprotect()
Dim Pwd As String
Pwd = InputBox("Please enter password to unprotect this sheet....")
If Pwd = "" Then
Cancel = True
Else
Sheets("AOct").Unprotect (Pwd)
End If
End Sub

It works fine if you enter the correct password, and works fine if yo
press cancel.
My problem is if you type in the wrong password, a debug error appears
I want it so if the password in incorrect, a message comes up sayin
incorrect password.

Any idea's
 
M

Max

Try this slightly amended sub:
[ password = MyOath ]

Sub Unprotect()
Dim Pwd As String
Pwd = InputBox("Please enter password to unprotect this sheet....")
If Pwd = "" Then
Cancel = True
ElseIf Pwd = "MyOath" Then
Sheets("AOct").Unprotect (Pwd)
Else
MsgBox ("You entered an incorrect password. Re-check & try again.")
End If
End Sub
 
I

ianripping

this is what I thought I would have to do, but isnt there a way to do i
so I dont have to write the password in the scripting.

I want this because the password will change alot
 
G

Guest

This will loop until they put in the correct password or hit cancel

Sub UnprotectSheet(
On Error Resume Nex
Dim Pwd As Strin
Dim strWrongPasswordMsg As Strin
D
Pwd = InputBox(strWrongPasswordMsg & "Please enter password to unprotect this sheet...."
If Pwd = "" The
Cancel = Tru
Exit D
Els
Err.Clea
Sheets("Sheet1").Unprotect Pw
If Err.Number = 0 The
Exit D
End I
strWrongPasswordMsg = "Invalid password." & vbCrLf & vbCrL
End I
Loo
End Su

-Brad
 
I

ianripping

This is close. But not quite there.

It will just cancel, instead of displaying a message when the incorrec
password is entered
 
I

ianripping

This is a tough one now....

Is there a way now to create a macro that will protect the sheet wit
password used to unprotect the sheet in the above macro
 
B

Bob Phillips

Hi Ian,

Try this

Sub Unprotect()
Dim Pwd As String
Dim Cancel As Boolean

Cancel = False
Do Until Cancel
Pwd = InputBox("Please enter password to unprotect this sheet....")
If Pwd = "" Then
Cancel = True
Exit Do
Else
On Error GoTo wrong_pass
Sheets("AOct").Unprotect (Pwd)
Exit Do
End If
wrong_pass:
MsgBox "Invalid password"
Loop
End Sub




--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
I

ianripping

Thanks Bob now I want to know:

Is there a way now to create a macro that will protect the sheet wit
password used to unprotect the sheet in the above macro
 
G

Guest

Dimension the password string in the declaration section, instead of the UnprotectSheet() module

Dim Pwd As Strin
Sub UnprotectSheet(
On Error Resume Nex
Dim strWrongPasswordMsg As Strin
D
Pwd = InputBox(strWrongPasswordMsg & "Please enter password to unprotect this sheet...."
If Pwd = "" The
Cancel = Tru
Exit D
Els
Err.Clea
Sheets("Sheet1").Unprotect Pw
If Err.Number = 0 The
Exit D
End I
strWrongPasswordMsg = "Invalid password." & vbCrLf & vbCrL
End I
Loo
End Su
Sub ProtectSheet(
If Pwd <> "" The
Sheets("Sheet1").Protect Pw
End I
End Su

-Brad
 
B

Bob Phillips

Hi Ian,

just save the valid password in a public variable.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top