Hide/Unhide rows based on a value on a protected sheet.

MitchiBoy3

New Member
Joined
Feb 24, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Good day experts!

I have this simple excel that will hide and unhide specific rows based on a value (data list). When my sheet is protected, I cant hide and unhide rows. Below is my VBA code.

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("bd5").Value = 1 Then
Rows("9:10").EntireRow.Hidden = False
Rows("14:30").EntireRow.Hidden = True
ElseIf Range("bd5").Value = 2 Then
Rows("9:10").EntireRow.Hidden = False
Rows("14:30").EntireRow.Hidden = False
Rows("14:14").EntireRow.Hidden = True
Rows("17:30").EntireRow.Hidden = True
ElseIf Range("bd5").Value = 3 Then
Rows("14:30").EntireRow.Hidden = False
Rows("14:15").EntireRow.Hidden = True
Rows("18:30").EntireRow.Hidden = True
Rows("9:10").EntireRow.Hidden = True
ElseIf Range("bd5").Value = 4 Then
Rows("9:10").EntireRow.Hidden = False
Rows("14:30").EntireRow.Hidden = False
Rows("14:16").EntireRow.Hidden = True
Rows("22:30").EntireRow.Hidden = True
ElseIf Range("bd5").Value = 5 Then
Rows("9:10").EntireRow.Hidden = False
Rows("14:30").EntireRow.Hidden = False
Rows("14:21").EntireRow.Hidden = True
Rows("28:30").EntireRow.Hidden = True
Rows("9:10").EntireRow.Hidden = True
ElseIf Range("bd5").Value = 6 Then
Rows("9:10").EntireRow.Hidden = False
Rows("14:30").EntireRow.Hidden = False
Rows("14:16").EntireRow.Hidden = True
Rows("22:30").EntireRow.Hidden = True
End If


If Range("BA8").Value = 1 Then
Rows("47:101").EntireRow.Hidden = True
ElseIf Range("BA8").Value = 2 Then
Rows("47:101").EntireRow.Hidden = True
Rows("47:101").EntireRow.Hidden = False
Rows("58:101").EntireRow.Hidden = True
ElseIf Range("BA8").Value = 3 Then
Rows("47:101").EntireRow.Hidden = True
Rows("47:101").EntireRow.Hidden = False
Rows("69:101").EntireRow.Hidden = True
ElseIf Range("BA8").Value = 4 Then
Rows("47:101").EntireRow.Hidden = True
Rows("47:101").EntireRow.Hidden = False
Rows("80:101").EntireRow.Hidden = True
ElseIf Range("BA8").Value = 5 Then
Rows("47:101").EntireRow.Hidden = True
Rows("47:101").EntireRow.Hidden = False
Rows("91:101").EntireRow.Hidden = True
ElseIf Range("BA8").Value = 6 Then
Rows("47:101").EntireRow.Hidden = True
Rows("47:101").EntireRow.Hidden = False

End If
End Sub


Im not expert on VBA but slightly knowledgeable. Please help me on this. Thanks so much!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

MitchiBoy3

New Member
Joined
Feb 24, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Welcome to the Board!


Yes, that is correct. You have to unprotect it in your VBA code first, do your hiding/unhiding, then re-protect it.
Here is code that shows you how to do that: VBA Protect / Unprotect Worksheets - Automate Excel
Thank you for the response.

Correct me if I'm wrong, is there a possibility to code that the sheet will be automatically protected and unprotected based on values?. I was thinking of this sequence,
1. A1 contains a value = 1;
2. Sheet will be unprotected;
3. Hide and unhide selected rows;
4. Sheet will be protected.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,927
Office Version
  1. 365
Platform
  1. Windows
Correct me if I'm wrong, is there a possibility to code that the sheet will be automatically protected and unprotected based on values?. I was thinking of this sequence,
1. A1 contains a value = 1;
2. Sheet will be unprotected;
3. Hide and unhide selected rows;
4. Sheet will be protected.
Yes. Just put the code referenced in the link in my previous post within an IF...THEN block.
It would be structured something like this (names of worksheet and passwords changed to match your situation, obviously).
VBA Code:
If Range("A1") = 1 Then
    Worksheets("Sheet1").Unprotect "Password"
    'Your hide/unhide code here
    Worksheets("Sheet1").Protect "Password"
End If
 
Solution

MitchiBoy3

New Member
Joined
Feb 24, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Yes. Just put the code referenced in the link in my previous post within an IF...THEN block.
It would be structured something like this (names of worksheet and passwords changed to match your situation, obviously).
VBA Code:
If Range("A1") = 1 Then
    Worksheets("Sheet1").Unprotect "Password"
    'Your hide/unhide code here
    Worksheets("Sheet1").Protect "Password"
End If
Thanks so much Joe! your're a blessing :D
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,927
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,396
Messages
5,769,841
Members
425,574
Latest member
grimeslisa

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
Top