Multiply VBA Macro for hiding and unhiding rows

Status
Not open for further replies.

Bram1212

New Member
Joined
Apr 12, 2018
Messages
36
Hi!

I found this VBA code that helps me hide/show a specific range when selecting one or the other cell. However I would like to use this multiple times on different cells. For instance when selecting B35 hide row 36:38 and when selecting D35 showing row 36:38. When selecting B39 hide row 40:42 and when selecting D39 showing row 40:42 and so on.

If it would also be possible to have it only trigger on CNTRL+select cell would be awesome.

Current code:

Option Explicit
Private Sub worksheet_selectionchange(ByVal target As Range)
If Selection.Count = 1 Then
If Not Intersect(target, Range("B31")) Is Nothing Then
Call HideRangeB31
ElseIf Not Intersect(target, Range("D31")) Is Nothing Then
Call HideRangeD31
End If
End If
End Sub

Private Sub HideRangeB31()
Rows("32:34").Hidden = True
End Sub

Private Sub HideRangeD31()
Rows("32:34").Hidden = False
End Sub

Thanks in advance for any tips.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .CountLarge = 1 Then
            If .Column = 2 Then
                Rows(.Row + 1 & ":" & .Row + 2).Hidden = True
            ElseIf .Column = 4 Then
                Rows(.Row + 1 & ":" & .Row + 2).Hidden = False
            End If
        End If
    End With
End Sub
 
Upvote 0
Hi John,

Thanks for your reply. I see now that every click on a cell in column B or D will hide/show the 3 rows below it. That is not exactly what I was going for.

My workaround is to have a smarter/cleaner way for the 'group/ungroup' function in Excel without having those + symbols in my sheet.

Therefore I do not need it to work on every cell but on every 4 lines since the three lines below would be optional and therefore I would like to hide/show them when necessary.

So row 31 is the first where this applies and then the clicking function of column B&D need to hide/show row 32:34
Then row 35 same for 36:38
Row 39 40:42
Row 43 44:46

And so on, last row is 123 which then needs to hide/show 124:126.

Hope I am making any sense :)
 
Upvote 0
Hi,
not fully tested but see if this code will do what you want

Place in your worksheets CODE PAGE

Rich (BB code):
Dim KeyCells        As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim r As Long
    
    If Target.CountLarge > 1 Then Exit Sub
    
    If KeyCells Is Nothing Then
        For r = 31 To 123 Step 4
            If KeyCells Is Nothing Then
                Set KeyCells = Union(Cells(r, 2), Cells(r, 4))
            Else
                Set KeyCells = Union(KeyCells, Cells(r, 2), Cells(r, 4))
            End If
        Next r
    End If
    
    If Not Intersect(Target, KeyCells) Is Nothing Then
        Rows(Target.Row + 1 & ":" & Target.Row + 3).Hidden = Target.Column = 2
    End If
    
End Sub

Note the variable at the top of the code. This must be placed at very TOP of your worksheets code page OUTSIDE any procedure

Dave
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top