简体   繁体   中英

Hide/Unhide rows with VBA - multiple ranges

I would like to hide/unhide rows with a click of a button. I have achieved this by using the following code:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)       

omrade = ActiveCell.Row + 4 & ":" & ActiveCell.Row + 37

   If Rows(omrade).EntireRow.Hidden = True Then
     Rows(omrade).EntireRow.Hidden = False
   Else
     Rows(omrade).EntireRow.Hidden = True
   End If

Exit Sub
End Sub

It works if the cell is hyperlinked to the same cell (eg cell D5 is hyperlinked to D5 - this would toggle the visibility of row 9 - 42).

However, I have multiple links (>100) and I do not want to manually enter each Cell Reference in the hyperlink. I have tried making dynamic hyperlinks using the Hyperlink-formula, but then my VBA code won't run.

EDIT: Tried to clarify my issue.

Try something like this:

Rows(omrade).EntireRow.Hidden = not Rows(omrade).EntireRow.Hidden

This would hide if visible and unhide if hidden.

Try this out:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim rngHyperlinkCell As Range
    Dim omrade As Range

    Set rngHyperlinkCell = Target.Range
    Set omrade = rngHyperlinkCell.Parent.Rows(rngHyperlinkCell.Row + 4 & ":" & rngHyperlinkCell.Row + 37)

    If omrade.EntireRow.Hidden = True Then
        omrade.EntireRow.Hidden = False
    Else
        omrade.EntireRow.Hidden = True
    End If

End Sub

I was wondering whether to post this as it's very similar to @Robin, and also uses the Hidden that @Vityata used:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim subAddress As Range
    Dim omrade As Range

    'Set subAddress = Range(Target.subAddress) 'Where the link looks at.
    Set subAddress = Target.Range   'Where the link is.

    With subAddress.Parent
        Set omrade = .Range(.Cells(subAddress.Row + 4, 1), .Cells(subAddress.Row + 37, 1))

        omrade.EntireRow.Hidden = Not omrade.EntireRow.Hidden

    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM