简体   繁体   English

如果单元格值为0,则excel vba切换隐藏/取消隐藏多个工作表中的范围行

[英]excel vba toggle hide/unhide range rows across multiple sheets, if cell value 0

i would like the below work across sheets "Summary", "W1", "W2", "W3","W4","W5" but I am facing some issues, can you please help. 我希望下面的工作跨页“摘要”,“W1”,“W2”,“W3”,“W4”,“W5”,但我面临一些问题,你能帮忙吗?

The code is to hide empty rows across the above sheets, to hide unused rows. 代码是隐藏上面工作表中的空行,以隐藏未使用的行。 The above ranges are identical across all of the above sheets. 以上范围在所有上述片材中是相同的。 The button is to toggle hide/unhide rows. 该按钮用于切换隐藏/取消隐藏行。

Thank you! 谢谢!

link to my file: my workbook 链接到我的文件: 我的工作簿

Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Less Rows" Then
CommandButton1.Caption = "More Rows"
Else
CommandButton1.Caption = "Less Rows"
End If
On Error Resume Next
  With Range("a8:a91,a96:a121").SpecialCells(xlBlanks).EntireRow
    .Hidden = Not .Hidden
  End With
  On Error GoTo 0
End Sub

This loops over all the rows in the range and checks for the values and also loop over all the sheets in the workbook. 这将循环遍历范围中的所有行并检查值,并循环遍历工作簿中的所有工作表。

Private Sub CommandButton1_Click()

Dim rng As Range
Dim iRow as Range
Dim wsh As Worksheet
Dim hidden_status As Boolean

If CommandButton1.Caption = "Less Rows" Then
   CommandButton1.Caption = "More Rows"
Else
   CommandButton1.Caption = "Less Rows"
End If

On Error Resume Next

For Each wsh in ThisWorkbook.Worksheets
   Set rng = wsh.Range("A8:A91,A96:A121") 
    For Each iRow in rng.Rows
        If iRow.Value = "" Then
           With iRow.EntireRow
                 hidden_status = .Hidden
                .Hidden = Not hidden_status
           End With
        End If
    Next iRow
Next wsh

On Error GoTo 0

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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