Macro fails if I use the button

Kappy

Board Regular
Joined
Jun 26, 2009
Messages
58
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Kappy
First up, it works fine for me if I press the Cancel Button.
Secondly, if you are concerned about the fact that the error might be caused by a button change COMMENT out those lines and see what happens, but I can't see that being an issue.
Also try replacing the PW lines with this
Code:
If PW <> "654321" Then Exit Sub
If PW = 654321 Then
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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