View Full Version : InputBox OK and Cancel Button Problem
mackypogi
05-26-2014, 07:36 AM
I have this macro code that can protect and unprotect, It will show the input box and ask for a password. my problem is when I choose cancel, it still protect the file, the same thing with my unprotect code. here is my code below
Protect Code
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd
Next wSheet
End Sub
UnProtect Code
Sub UnProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
On Error Resume Next
For Each wSheet In Worksheets
wSheet.Unprotect Password:=Pwd
Next wSheet
If Err <> 0 Then
MsgBox "You have entered an incorect password. All worksheets could not " & _
"be unprotected.", vbCritical, "Incorect Password"
End If
On Error GoTo 0
End Sub
princ_wns
05-26-2014, 02:05 PM
Try this
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
If Pwd <> "" Then
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd
Next wSheet
End If
End Sub
Sub UnProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
On Error Resume Next
If Pwd <> "" Then
For Each wSheet In Worksheets
wSheet.Unprotect Password:=Pwd
Next wSheet
If Err <> 0 Then
MsgBox "You have entered an incorect password. All worksheets could not " & _
"be unprotected.", vbCritical, "Incorect Password"
End If
End If
On Error GoTo 0
End Sub
LalitPandey87
05-26-2014, 04:07 PM
Try this also:
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
If Pwd = vbNullString Then GoTo ExitPoint
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd
Next wSheet
ExitPoint:
'Memory cleaning
Set wSheet = Nothing
Pwd = vbNullString
End Sub
Sub UnProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
If Pwd = vbNullString Then GoTo ExitPoint
On Error GoTo ExitPoint
For Each wSheet In Worksheets
wSheet.Unprotect Password:=Pwd
Next wSheet
ExitPoint:
If Err <> 0 Then
MsgBox "You have entered an incorect password. All worksheets could not " & _
"be unprotected.", vbCritical, "Incorect Password"
On Error GoTo -1: On Error GoTo 0: Err.Clear
End If
'Memory cleaning
Set wSheet = Nothing
Pwd = vbNullString
End Sub
Ingolf
05-26-2014, 06:37 PM
This is cross post
InputBox OK and Cancel Button Problem (http://www.vbaexpress.com/forum/showthread.php?49717-InputBox-OK-and-Cancel-Button-Problem)
You got the answer here but did not answer
bakerman
05-28-2014, 01:50 AM
Dear mackypogi,
As you can see cross-posting isn't very much appreciated. Read following link regarding this issue.
http://www.excelfox.com/forum/f25/message-to-cross-posters-1172/
Rick Rothstein
05-30-2014, 12:20 AM
Although this thread is effectively closed, I thought I would use it to introduce those reading it to my latest mini-blog article here which shows how to be able to react to the Cancel button in an VBA InputBox...
How To React To The Cancel Button in a VB (not Application) InputBox (http://www.excelfox.com/forum/f22/how-to-react-to-the-cancel-button-in-a-vbulletin-not-application-inputbox-1828/)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.