mcarterlgk
New Member
- Joined
- Mar 10, 2014
- Messages
- 6
I have a sheet that needs to have columns hidden if the value in row 35 of the column is <0 and unhide the columns if isn't(The values in row 35 are calculated based on B8)
If B8<2 then rows 64-97 need to be hidden as well and unhiden if not.
I had code that worked at one point, but something changed and the code no longer automatically triggers the changes.
The code stopped working after I protected the sheet and no longer worked after I unprotected it. I do not know if this is the issue.
I would like the code to automatically unprotect and reprotect the sheet after hiding/unhiding the rows and columns. (if it necessary to unprotect to hide/unhide. I though it was, but I could be incorrect.)
My code is a bit of a mess as I am not normally a programmer and it was cobbled together through snippets found on mrexcel and elsewhere as well as advice from a previous posting.
my code:
If B8<2 then rows 64-97 need to be hidden as well and unhiden if not.
I had code that worked at one point, but something changed and the code no longer automatically triggers the changes.
The code stopped working after I protected the sheet and no longer worked after I unprotected it. I do not know if this is the issue.
I would like the code to automatically unprotect and reprotect the sheet after hiding/unhiding the rows and columns. (if it necessary to unprotect to hide/unhide. I though it was, but I could be incorrect.)
My code is a bit of a mess as I am not normally a programmer and it was cobbled together through snippets found on mrexcel and elsewhere as well as advice from a previous posting.
my code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("B8")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
Dim col As Range
Dim rng As Range
Dim x As Long
Set rng = ActiveSheet.UsedRange
x = 35
'Hide columns based on value in row 35
For Each col In rng.Columns
y = col.Column
If Cells(x, y) < 0 And y > 2 Then col.EntireColumn.Hidden = True Else: col.EntireColumn.Hidden = False
Next col
'Hide rows if B8 is < 2
Application.EnableEvents = True
If Target.Address = "$B$8" Then
If Range("B8") < 2 Then
Rows("64:97").Hidden = True
End If
If Range("B8") > 1 Then
Rows("64:97").Hidden = False
End If
End If
End Sub