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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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