Home > OS >  VBA Excel Button to Hide/Unhide Cells with password protection
VBA Excel Button to Hide/Unhide Cells with password protection

Time:02-16

I want to Hide or Unhide Cells, (not worksheet, I can use Range), using VBA and protect the button so to hide or unhide needs a password to make it work. Can you help?

Thank you!

CodePudding user response:

Call a inputbox and set the password there on buttonclick, if correct run your code.

CodePudding user response:

Option Explicit

Private Sub Rectangle1_Click()

Dim pass As String, pass1 As String, x As String ' Set you variables
x = 1                           ' Used as a counter for number of tries
pass1 = "abC!23"                            ' Set you password!
pass = InputBox("Passcode?", "Attempt "   x)' Display and ask for the 
'first login attempt
Do While (Not pass = pass1)    ' Do while input does NOT match the passcode
    x = x   1                 ' Add one attempt
    If x > 3 Then Exit Sub    ' If there are more than 3 attempts then exit 
'the code
    pass = InputBox("Wrong passcode, try again", "Attempt "   x   "/3") ' 
'Reset the pass variabel with another attempt, x keeps tabs on how many 
'tries
Loop                                         ' Loop -> return to Do While

MsgBox ("Login succeeded") '<----delete this messagebox if you want and
'-----run your code upon correct pass here-----
    

End Sub

  • Related