Password Protect Command Button

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
359
Office Version
  1. 365
Platform
  1. Windows
Hi all I have a workbook that I need to share out to my wife and there are a couple of command buttons that once clicked would enable her to be able to view the coding ect of the WB as well as the ribbon and tabs ect.

The code shown below using a inputbox, prompts for a password to be entered if the button is clicked BUT if the password is wrong once OK is clicked or Cancel is selected then the inputbox closes and the code runs anyway.
Would someone let me know what I need to add to react to a wrong password entry and also to the cancel button.

VBA Code:
Private Sub cmbEditWB_Click()

Dim 123 As String

    123 = InputBox("Please Enter Password to Access Controls")
    If ans <> "123" Then
    
    Call EditWB
    
    End If
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If you cancel or put in the wrong value, it will never be equal to 123, so of course your code will run. You'd want to run it IF it equals 123.
This simplified version should work.
VBA Code:
Private Sub cmbEditWB_Click()
Dim ans As String

ans = InputBox("Please Enter Password to Access Controls")
If ans = "123" Then EditWB
   
End Sub
 
Upvote 0
Solution
Happens to everyone. If you have a solution, please mark it a solved. That will take this off of the list of threads with no solution.
 
Upvote 0
Glad to help. But you marked your acceptance post as the solution rather than the post containing the solution.
 
Upvote 0
Thank you - I can't believe I missed the obvious
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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