I would prefer to use a double-click function. I am using this code to both hide the empty cells between two cells with 'account numbers' and to double-click some color in other cells.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim HideRows As Boolean
If Target.Column >= 4 Then
Cancel = True
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case Else: Target.Interior.ColorIndex = 4
End Select
End If
If Application.Intersect(Target, Range("B:B")) Is Nothing Then Cancel = True: Exit Sub
If Rows(Target.Offset(1, 0).Row).Hidden Then _
HideRows = False Else HideRows = True
'if there is no cell with a value below the clicked cell then cancel
If Cells.Rows.Count = Target.End(xlDown).Row Then Cancel = True _
Else Range(Target.Offset(1, 0), Target.End(xlDown).Offset(-1, 0)).Rows.Hidden = HideRows
End Sub
The problem is that this VBA opens all the hidden cells below it instead of just the cell clicked. Any ideas?
Bookmarks