Any suggestions why the following code is not triggered when I change the value of cell A1 to TRUE or FALSE? The code is in the active worksheet. All suggestions to make the code more elegant are welcome too.
Thanks!
Rich (BB code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then
Exit Sub
End If
Application.enableevents = False
Dim h As Long, i As Long, j As Long, k As Long
Dim Rng As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' h is the row index of the selected range (looping)
' i is the row index of first non-header row of selected range
' j is the row index of the last row of the selected range
' k is the kth member of the Range Rng
'
' Rng is the union of rows in the selected range where
' column A has the value "TRUE" <= to be hidden
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Target.Address = "$A$1" And Target.Value = True Then
With ActiveSheet.Cells(4, 2).CurrentRegion
i = 5 '.Item(2, 1).Row 'FIRST ROW OF DATA
j = .Item(.Item.Count).Row 'LAST ROW OF DATA
End With
k = 0
For h = i To j Step 1
If ActiveSheet.Cells(h, 1) = True Then
k = k + 1
If k = 1 Then
Set Rng = ActiveSheet.Cells(h, 1)
Debug.Print Rng.Address
Else
Set Rng = Union(Rng, ActiveSheet.Cells(h, 1))
Debug.Print Rng.Address
End If
End If
Next h
Rng.EntireRow.Hidden = True
ElseIf Target.Address = "$A$1" And Target.Value = False Then
ActiveSheet.Cells.EntireRow.Hidden = False
End If
Application.enableevents = True
End Sub