Calling all excel wizards- I am new to macros but I need to get this right for a project at work.! I am trying to create a macro that expands and collapsesrows based on a cell value. The macrothat I wrote below works like a charm EXCEPT I have to click out of the targetcell and click it again to expand or collapse.
Is there a simple way to fix this? IÂ’m willing to change thewhole macro if need be.
HereÂ’s the information:
My Target Cell is A5. When I click A5 I need the macro tosearch column J and hide every row that does not have an “87” in column J.
In column “J” I’ve put an “87” for the rows that I do notwant to hide and a “77” for the rows that I want hidden
The cell value for A5 shows as “-” when the rows areexpanded and “+“when the rows are collapsed.
My current macro says that if the cell value for A5 is “+”then run macro “Expand_LargeCap” whichhide every row that has “77” in column J.
Again these VBAS work fine together EXCEPT When I click A5 and it expands the rows I have to click anothercell and thennn click A5 again to collapse. How can I do it with just clickingA5 once to expand and clicking A5 againto collapse without having to click an empty cell in between
See below
Is there a simple way to fix this? IÂ’m willing to change thewhole macro if need be.
HereÂ’s the information:
My Target Cell is A5. When I click A5 I need the macro tosearch column J and hide every row that does not have an “87” in column J.
In column “J” I’ve put an “87” for the rows that I do notwant to hide and a “77” for the rows that I want hidden
The cell value for A5 shows as “-” when the rows areexpanded and “+“when the rows are collapsed.
My current macro says that if the cell value for A5 is “+”then run macro “Expand_LargeCap” whichhide every row that has “77” in column J.
Again these VBAS work fine together EXCEPT When I click A5 and it expands the rows I have to click anothercell and thennn click A5 again to collapse. How can I do it with just clickingA5 once to expand and clicking A5 againto collapse without having to click an empty cell in between
See below
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A5:A5")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Value = "-" Then
Collapse_LargeCap
ElseIf Target.Value = "+" Then
Expand_LargeCap
End
End If
End If
Sub Expand_LargeCap()
Dim cell As Range
For Each cell In Range("j5:j11")
If UCase(cell.Value) > 0 Then
cell.EntireRow.Hidden = False
End If
Next
For Each cell In Range("A5:A5")
cell.Value = ("-")
Next cell
End Sub
Sub Collapse_LargeCap()
Dim cell As Range
For Each cell In Range("j5:j11")
If UCase(cell.Value) = 77 And cell.EntireRow.Hidden = False Then
cell.EntireRow.Hidden = True
End If
Next
For Each cell In Range("A5:A5")
cell.Value = ("+")
Next cell
End Sub
Last edited by a moderator: