Toggle Unhide rows but wants to prompt a password to use

dskyla

New Member
Joined
Oct 13, 2015
Messages
1
I want to be able show a prompt to add in the password before being able to do the actions below.

Private SubToggleButton2_Click()


If ToggleButton2Then
ActiveSheet.UnprotectPassword
ToggleButton2.ForeColor = RGB(0, 0, 0)
ToggleButton2.Font.Bold = False
ToggleButton2.Caption = "Hide"
Rows("7:13").EntireRow.Hidden =True
Rows("26:29").EntireRow.Hidden =True
Rows("47:53").EntireRow.Hidden =True
Columns("E:R").EntireColumn.Hidden = True
ActiveSheet.Protect
Else
ActiveSheet.UnprotectPassword
ToggleButton2.ForeColor = RGB(0, 0, 255)
ToggleButton2.Font.Bold = True
ToggleButton2.Caption = "Unhide"
Rows("7:13").EntireRow.Hidden =False
Rows("26:29").EntireRow.Hidden =False
Rows("47:53").EntireRow.Hidden =False
Columns("E:R").EntireColumn.Hidden = False
ActiveSheet.Protect
End If

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi welcome to the board.

See if this code does what you want:

Rich (BB code):
Private Sub ToggleButton2_Click()
    Dim UserPassword As String
    Dim HideRows As Boolean
    
    'add your password here
    UserPassword = "mypassword"
    
    ActiveSheet.Unprotect Password:=UserPassword
    
    With ToggleButton2
    
    If .Value Then HideRows = IsValidPassword(Password:=UserPassword)
        .ForeColor = IIf(HideRows, RGB(0, 0, 0), RGB(0, 0, 255))
        .Font.Bold = Not HideRows
        .Caption = IIf(HideRows, "Unhide", "Hide")
        Rows("7:13").EntireRow.Hidden = HideRows
        Rows("26:29").EntireRow.Hidden = HideRows
        Rows("47:53").EntireRow.Hidden = HideRows
        Columns("E:R").EntireColumn.Hidden = HideRows
        .Value = HideRows
    End With
    
    ActiveSheet.Protect Password:=UserPassword


End Sub


Function IsValidPassword(ByVal Password As String) As Boolean
    Dim Entry As Variant
    Do
    Entry = InputBox("Enter Password", "Password")
    If StrPtr(Entry) = 0 Then Exit Function
        IsValidPassword = Entry = Password
        If Not IsValidPassword Then MsgBox "Password Invalid", 48, "Error"
    Loop Until IsValidPassword
End Function

Add your password where shown in RED.

Whilst I think I have the logic of your requirement correct you should adjust code as required.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,239
Members
448,951
Latest member
jennlynn

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