This is by far the weirdest issue I've encountered in my short time as a VBA user..
I have a code that uses and Inputbox (and works totally fine)..
except when the user presses cancel..
so I looked into it, and added code to handle cancel (if the response = "")
Here's the weird part.. If I step through the code using F8 or if I press the play button in the VBE, I get NO error when I press cancel on the input box...
BUT, if I run the macro using the button I created and press cancel on the inputbox, I get an error: "Type Mismatch"
I imagine this has something to do with the fact that my macro involves changing the text in the button (but I really don't understand why this is an issue, since in a "cancel" event, I've told it to exit sub)
I'm posting my code below, I'm completely stumped. Please help!
Sub LockTop()
Dim PW As String
PW = InputBox("A password is required to lock or unlock the top portion of this sheet" & vbNewLine _
& vbNewLine & "Please enter the password below", "Password Required")
If PW = "" Then Exit Sub
If PW = 654321 Then
ActiveSheet.Unprotect Password:="David"
If Range("D7").Locked = False Then
Range("D7:D13").Locked = True
Range("I5:I11").Locked = True
Else
Range("D7:D13").Locked = False
Range("I5:I11").Locked = False
End If
If Range("D7").Locked = True Then
Sheets("Costing Tool").Shapes("Button 9").TextFrame.Characters.Text = "Unlock Plans"
Else
Sheets("Costing Tool").Shapes("Button 9").TextFrame.Characters.Text = "Lock Plans"
End If
ActiveSheet.Protect Password:="David"
Else
MsgBox "Sorry, the password you have entered is not valid"
End If
End Sub
I have a code that uses and Inputbox (and works totally fine)..
except when the user presses cancel..
so I looked into it, and added code to handle cancel (if the response = "")
Here's the weird part.. If I step through the code using F8 or if I press the play button in the VBE, I get NO error when I press cancel on the input box...
BUT, if I run the macro using the button I created and press cancel on the inputbox, I get an error: "Type Mismatch"
I imagine this has something to do with the fact that my macro involves changing the text in the button (but I really don't understand why this is an issue, since in a "cancel" event, I've told it to exit sub)
I'm posting my code below, I'm completely stumped. Please help!
Sub LockTop()
Dim PW As String
PW = InputBox("A password is required to lock or unlock the top portion of this sheet" & vbNewLine _
& vbNewLine & "Please enter the password below", "Password Required")
If PW = "" Then Exit Sub
If PW = 654321 Then
ActiveSheet.Unprotect Password:="David"
If Range("D7").Locked = False Then
Range("D7:D13").Locked = True
Range("I5:I11").Locked = True
Else
Range("D7:D13").Locked = False
Range("I5:I11").Locked = False
End If
If Range("D7").Locked = True Then
Sheets("Costing Tool").Shapes("Button 9").TextFrame.Characters.Text = "Unlock Plans"
Else
Sheets("Costing Tool").Shapes("Button 9").TextFrame.Characters.Text = "Lock Plans"
End If
ActiveSheet.Protect Password:="David"
Else
MsgBox "Sorry, the password you have entered is not valid"
End If
End Sub