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

MitchiBoy3

New Member
Joined
Feb 24, 2020
Messages
4
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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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.
 
Upvote 0
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
 
Upvote 0
Solution
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
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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