隐藏工作表中多个区域中没有数据的行

时间:2019-03-08 14:39:26

标签: excel vba

我最近刚刚使该代码生效;

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Application.ScreenUpdating = False
    With Range("G24:G71,N24:N71")
        Dim r As Long: For r = 1 To .Areas(1).Rows.Count
            Dim bHide As Boolean: bHide = True
            Dim xArea As Range: For Each xArea In .Areas
                If IsEmpty(xArea.Cells(r, 1).Value) = False Then
                    bHide = False
                End If
            Next xArea
            .Rows(r).EntireRow.Hidden = bHide
        Next r
    End With
    Application.ScreenUpdating = True
End Sub

但是,我希望代码在多个领域进行研究。我已经尝试了以下方法,但它不起作用;

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Application.ScreenUpdating = False
    With Range("G24:G71,N24:N71")
        Dim r As Long: For r = 1 To .Areas(1).Rows.Count
            Dim bHide As Boolean: bHide = True
            Dim xArea As Range: For Each xArea In .Areas
                If IsEmpty(xArea.Cells(r, 1).Value) = False Then
                    bHide = False
                End If
            Next xArea
            .Rows(r).EntireRow.Hidden = bHide
        Next r
    End With
    Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Application.ScreenUpdating = False
    With Range("G81:G124,N81:N124")
        Dim r As Long: For r = 1 To .Areas(1).Rows.Count
            Dim bHide As Boolean: bHide = True
            Dim xArea As Range: For Each xArea In .Areas
                If IsEmpty(xArea.Cells(r, 1).Value) = False Then
                    bHide = False
                End If
            Next xArea
            .Rows(r).EntireRow.Hidden = bHide
        Next r
    End With
    Application.ScreenUpdating = True
End Sub

我收到以下错误消息;

  

编译错误:检测到不明确的名称:Worksheet_PivotTableUpdate。

它还会突出显示第二个代码字符串的第一行

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为这应该可行,前提是您的代码按您当前的预期运行。

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Application.ScreenUpdating = False
    With Range("G24:G71,N24:N71")
        Dim r As Long: For r = 1 To .Areas(1).Rows.Count
            Dim bHide As Boolean: bHide = True
            Dim xArea As Range: For Each xArea In .Areas
                If IsEmpty(xArea.Cells(r, 1).Value) = False Then
                    bHide = False
                End If
            Next xArea
            .Rows(r).EntireRow.Hidden = bHide
        Next r
    End With
    With Range("G81:G124,N81:N124")
        For r = 1 To .Areas(1).Rows.Count
            bHide = True
            For Each xArea In .Areas
                If IsEmpty(xArea.Cells(r, 1).Value) = False Then
                    bHide = False
                End If
            Next xArea
            .Rows(r).EntireRow.Hidden = bHide
        Next r
    End With
    Application.ScreenUpdating = True
End Sub