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
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,816
Office Version
2019
Platform
Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,099,087
Messages
5,466,551
Members
406,489
Latest member
Ankusharma

This Week's Hot Topics

Top