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

时间:2017-04-17 16:29:40

标签: excel vba excel-vba

我希望以下工作跨页“摘要”,“W1”,“W2”,“W3”,“W4”,“W5” 但我面临一些问题,请你帮忙。

代码是隐藏上面工作表中的空行,以隐藏未使用的行。 以上范围在所有上述片材中是相同的。 该按钮用于切换隐藏/取消隐藏行。

谢谢!

链接到我的文件: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

1 个答案:

答案 0 :(得分:0)

这将遍历范围中的所有行并检查值,并循环遍历工作簿中的所有工作表。

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