循环将计算字段添加到多个数据透视表

时间:2018-02-06 15:35:59

标签: excel vba excel-vba

我正在尝试将两个calced字段添加到8个工作表中的24个数据透视表中。它基于在Sheet 1的B4中选择数据验证列表。

删除当前两个已调整字段的代码正在运行:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim sht As Worksheet
Dim pt As PivotTable
Dim pf As PivotField

On Error Resume Next
If Intersect(Target, Sheet1.Range("B4")) Is Nothing Then Exit Sub

For Each sht In ThisWorkbook.Worksheets
    For Each pt In sht.PivotTables
        For Each pf In pt.DataFields
            pf.DataRange.Cells(1, 1).PivotItem.Visible = False
        Next pf
    Next pt
Next sht

接下来,我需要添加两个我无法工作的新计算字段:

If Sheet1.Range("B4") = "Hourly Utilization" Then
    For Each sht In ThisWorkbook.Worksheets
        For Each pt In sht.PivotTables
            With pt
                .AddDataField sht.PivotTables( _
                "*").PivotFields("hClientUtil"), "Client"
                .DataBodyRange.NumberFormat = "#0.0%"

                .AddDataField sht.PivotTables( _
                "*").PivotFields("hProdUtil"), "Productive"
                .DataBodyRange.NumberFormat = "#0.0%"
            End With
        Next pt
    Next sht
End If

如果按工作表和名称指定数据透视表,我可以使用它,但我希望能够循环它,而不是让代码重复24次。

我认为它可能与“*”有关,因为我不知道如何在不使用数据透视表名称的情况下指定数据透视表。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

编辑:我只有一半想出来......

为了提供更多背景,工作表1有12个数据透视表,工作表2-7每个都有1个,工作表8有5个。在具有多个透视表的工作表上,它们是垂直堆叠的 - 例如在工作表1上,PT1从A5开始,PT2在A15开始,PT3在A25开始,依此类推。

下面粘贴的代码适用于所有这些代码,但Sheet1上的最后5个表除外。我在所有数据透视表上都禁用了字段标题。奇怪的是,如果我在最后5个表上启用字段标题,则VBA可以正常工作。如果我在任何其他表上启用字段标题,它仍然有效。

我的问题是为什么代码会在某些带有或不带字段标题的表上运行,但只能在有问题的5个表上运行并启用字段标题?完整代码如下...

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Dim sht As Worksheet
Dim pt As PivotTable
Dim pf As PivotField

'VBA only runs if cell B4 on the Summary tab is changed

On Error Resume Next
If Intersect(Target, Sheet1.Range("B4")) Is Nothing Then Exit Sub

'Removes values from Pivot Tables

For Each sht In ThisWorkbook.Worksheets
    For Each pt In sht.PivotTables
        For Each pf In pt.DataFields
            pf.DataRange.Cells(1, 1).PivotItem.Visible = False
        Next pf
    Next pt
Next sht

'When Hourly Utilization is chosen

If Sheet1.Range("B4") = "Hourly Utilization" Then
    For Each sht In ThisWorkbook.Worksheets
        For Each pt In sht.PivotTables
            With sht.PivotTables(pt.Name)
                .AddDataField sht.PivotTables( _
                pt.Name).PivotFields("hClientUtil"), "Client"
                .AddDataField sht.PivotTables( _
                pt.Name).PivotFields("hProdUtil"), "Prod."
                .DataBodyRange.NumberFormat = "#0.0%"
            End With
        Next pt
    Next sht
End If

'When $ Weighted Utilization is chosen

If Sheet1.Range("B4") = "$ Weighted Utilization" Then
    For Each sht In ThisWorkbook.Worksheets
        For Each pt In sht.PivotTables
            With sht.PivotTables(pt.Name)
                .AddDataField sht.PivotTables( _
                 pt.Name).PivotFields("dClientUtil"), "Client"
                .AddDataField sht.PivotTables( _
                 pt.Name).PivotFields("dProdUtil"), "Prod."
                .DataBodyRange.NumberFormat = "#0.0%"
            End With
        Next pt
    Next sht
End If

End Sub