[英]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). 如果单元格被超链接到同一单元格(例如,单元格D5被超链接到D5-这将切换第9-42行的可见性),它将起作用。
However, I have multiple links (>100) and I do not want to manually enter each Cell Reference in the hyperlink. 但是,我有多个链接(> 100),并且我不想在超链接中手动输入每个单元格引用。 I have tried making dynamic hyperlinks using the Hyperlink-formula, but then my VBA code won't run.
我尝试使用Hyperlink公式制作动态超链接,但随后我的VBA代码将无法运行。
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: 我想知道是否要发布它,因为它与@Robin非常相似,并且还使用了@Vityata使用的Hidden:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.