Hi BL,
This seems to work...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngCheck As Range
Dim lLastRow As Long
Dim rngCell As Range
Dim I As Long
Application.EnableEvents = False
On Error GoTo ERROR_HANDLER
lLastRow = Range("D" & Range("D
").Rows.Count).End(xlUp).Row
Dim strCheckAddress As String
strCheckAddress = "D37"
For I = 49 To lLastRow Step 10
strCheckAddress = strCheckAddress & ", D" & I
Next I
Set rngCheck = Range(strCheckAddress)
For Each rngCell In rngCheck
Select Case rngCell.Row
Case 37
Range(Cells(37, 1), Cells(48, 1)).EntireRow.Hidden = _
IIf(rngCell.Value = 0, True, False)
Case Else
Range(Cells(rngCell.Row, 1), _
Cells(rngCell.Row + 9, 1)).EntireRow.Hidden = _
IIf(rngCell.Value = 0, True, False)
End Select
Next rngCell
Application.EnableEvents = True
Exit Sub
ERROR_HANDLER: Application.EnableEvents = True
End Sub
It's triggered whenever a cell on that sheet changes, so the zero
values governing the row hiding can be determined by a formula in those
D cells or directly entered into those D cells.
If you require that the code be triggered by any workbook calculation
then you could try...
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
On Error GoTo ERROR_HANDLER
Dim rngCheck As Range
Dim lLastRow As Long
Dim rngCell As Range
Dim I As Long
lLastRow = Range("D" & Range("D
").Rows.Count).End(xlUp).Row
Dim strCheckAddress As String
strCheckAddress = "D37"
For I = 49 To lLastRow Step 10
strCheckAddress = strCheckAddress & ", D" & I
Next I
Set rngCheck = Range(strCheckAddress)
For Each rngCell In rngCheck
Select Case rngCell.Row
Case 37
Range(Cells(37, 1), Cells(48, 1)).EntireRow.Hidden = _
IIf(rngCell.Value = 0, True, False)
Case Else
Range(Cells(rngCell.Row, 1), _
Cells(rngCell.Row + 9, 1)).EntireRow.Hidden = _
IIf(rngCell.Value = 0, True, False)
End Select
Next rngCell
Application.EnableEvents = True
Exit Sub
ERROR_HANDLER: Application.EnableEvents = True
End Sub
The trouble here though is if you ever need to manually unhide the
hidden rows to get to a hidden cell to edit its value, the automatic
code just rehides any rows you try to unhide, so you would need to go
into the VBA editor and disable events in the immediate window.
Ken Johnson