Hiding/Unhiding a row in a Password Protected Spreadsheet

Chris Waller

Board Regular
Joined
Jan 18, 2009
Messages
183
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I am trying to find out if it is possible to hide/unhide a row on a password protected spreadsheet when the data changes in a cell?

I have a Spreadsheet that contains a lot of formulae. In order to prevent people messing with the formulae either inadvertently or maliciously I have protected all the Formulae, however, If cell F1 equals 2022/2023 I need row 71 to be visible, however, if the cell changes to any other value I need the row to be hidden. The row contains the extra banh holiday for the Queen's Platinum Jubilee, so after 2022/2023 this row is no longer required.

TIA
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Place this macro in the code module for ThisWorkbook:
Rich (BB code):
Private Sub Workbook_Open()
    With Sheets("Sheet1")
        .Unprotect
        If .Range("F1") = "2022/2023" Then
            .Rows(71).Hidden = False
        Else
            .Rows(71).Hidden = True
        End If
        .Protect
    End With
End Sub

Place this module in the code module for your worksheet.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F1")) Is Nothing Then Exit Sub
    With Sheets("Sheet1")
        .Unprotect
        .Rows(71).Hidden = True
        .Protect
    End With
End Sub
Change the sheet name (in red) to suit your needs.
 
Upvote 0
Place this macro in the code module for ThisWorkbook:
Rich (BB code):
Private Sub Workbook_Open()
    With Sheets("Sheet1")
        .Unprotect
        If .Range("F1") = "2022/2023" Then
            .Rows(71).Hidden = False
        Else
            .Rows(71).Hidden = True
        End If
        .Protect
    End With
End Sub

Place this module in the code module for your worksheet.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F1")) Is Nothing Then Exit Sub
    With Sheets("Sheet1")
        .Unprotect
        .Rows(71).Hidden = True
        .Protect
    End With
End Sub
Change the sheet name (in red) to suit your needs.
Morning mumps,

Thanks for the code above, it worked a treat, however, I keep being prompted for the password. Whilst this is OK while I'm using it, this is for someone else and they will not have my password, so is there a way around this?

I would like to keep the password secret so is there a way to automate the whole process?
 
Upvote 0
Add Password after .Unprotect and .Protect
VBA Code:
.Unprotect "password"


.protect "password"

AND
In the VBA main menu, click on Tools | VBAProject Properties and under the tab "Protection", Check the box "Lock project for viewing" and enter the Specific password to Hiding code from Viewing.
 
Upvote 0
Add Password after .Unprotect and .Protect
VBA Code:
.Unprotect "password"


.protect "password"

AND
In the VBA main menu, click on Tools | VBAProject Properties and under the tab "Protection", Check the box "Lock project for viewing" and enter the Specific password to Hiding code from Viewing.
Maabadi,

Thanks for that. It worked a treat.
 
Upvote 0
Place this macro in the code module for ThisWorkbook:
Rich (BB code):
Private Sub Workbook_Open()
    With Sheets("Sheet1")
        .Unprotect
        If .Range("F1") = "2022/2023" Then
            .Rows(71).Hidden = False
        Else
            .Rows(71).Hidden = True
        End If
        .Protect
    End With
End Sub

Place this module in the code module for your worksheet.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F1")) Is Nothing Then Exit Sub
    With Sheets("Sheet1")
        .Unprotect
        .Rows(71).Hidden = True
        .Protect
    End With
End Sub
Change the sheet name (in red) to suit your needs.
Good Evening Mumps,

The code above was great and did what I needed it to do. I just have a query regarding the hiding and unhiding of row 71. I know your code will hide row 71 when I change the year to anything other than 2022/2023, however, if I then change back to 2022/2023 it does not then unhide row 71. Is this something that you could perhaps look at for me? TIA
 
Upvote 0
Please test this as worksheet change event:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F1")) Is Nothing Then Exit Sub
    With Sheets("Sheet1")
        .Unprotect "password"
        If .Range("F1") = "2022/2023" Then
            .Rows(71).Hidden = False
         Else
            .Rows(71).Hidden = True
        End If
       .Protect "password"
    End With
End Sub
 
Upvote 0
What about just this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  ActiveSheet.Unprotect Password:="password"
  Rows(71).Hidden = Range("F1").Value <> "2022/2023"
  ActiveSheet.Protect Password:="password"
End Sub
 
Upvote 0
Please test this as worksheet change event:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("F1")) Is Nothing Then Exit Sub
    With Sheets("Sheet1")
        .Unprotect "password"
        If .Range("F1") = "2022/2023" Then
            .Rows(71).Hidden = False
         Else
            .Rows(71).Hidden = True
        End If
       .Protect "password"
    End With
End Sub
Maabadi,

That's twice you have come to my rescue and another fantastic job. Brilliant I cannot thank you enough.
 
Upvote 0
Please test also Peter solution, it is smaller and work perfect.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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