用于测试枢轴字段的小计是否可见的功能

时间:2016-12-18 02:42:59

标签: excel-vba vba excel

我想要一个函数来告诉我枢轴字段的小计是否可见。例如,在此数据透视表中,Region和Item都打开了小计...

enter image description here

...但只显示Region小计,这是有道理的。我想要一个函数,如果传递的字段的小计实际显示在数据透视表中,则返回true。

此函数将pivot字段作为其参数,并返回是否打开小计。它对Region和Item字段都返回true,因为它们都打开了小计:

Function PivotFieldSubtotalsOn(pvtField As Excel.PivotField) As Boolean
Dim i As Long

For i = 1 To 12
    If pvtField.Subtotals(i) = True Then
        PivotFieldSubtotalsOn = True
        Exit Function
    End If
Next i
End Function

相反,我希望它在上面的情况下为Item返回False,因为Item子小计实际上没有显示。

我考虑过检查字段名称和单词“Total”是否出现在数据透视表中的单元格中,但是可以更改小计标签。我最好的猜测是添加一个测试字段Position是否不是Orientation的最后一个,因为在这种情况下小计不会显示。

1 个答案:

答案 0 :(得分:0)

我的回答还包括我的默认Sub,用于创建和更新PivotTable(您可以忽略或删除不需要的部分)。

以下代码循环遍历PivotFields中的所有PivotTable,并检查当前PivotField.Name = "Region"。下一步是检查PivotField.Subtotals(1) = True

所以我修改了你的函数以适应从Sub调用和使用它的方式,但在你的情况下,如果你只检查一个字段(“Region”),你可以把它Sub中的整个代码,但我不确定你的代码最终目标是什么。

功能PivotFieldSubtotalsOn代码

Function PivotFieldSubtotalsOn(pvtField As Excel.PivotField) As Boolean

If pvtField.Subtotals(1) = True Then
    PivotFieldSubtotalsOn = True
Else
    PivotFieldSubtotalsOn = False
End If

End Function

Sub AutoSetupPivot代码

Option Explicit

Sub AutoSetupPivot()

Dim PivotSht            As Worksheet
Dim SrcData             As Range
Dim Lrow                As Long, LCol       As Long
Dim PvtCache            As PivotCache
Dim PvtTbl              As PivotTable

' determine the data range you want for the pivot
With Worksheets("DATA")
    Lrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

    Set SrcData = .Range("A1:" & .Cells(Lrow, LCol).Address(False, False))
End With

' set destination Sheet for Pivot Table
Set PivotSht = ThisWorkbook.Sheets("PIVOT")

' set Pivot Cache
Set PvtCache = ThisWorkbook.PivotCaches.Add(xlDatabase, SrcData)

' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PvtTbl = PivotSht.PivotTables("PivotTable1") ' check if "PivotTable1" Pivot Table already created (in past runs of this Macro)

On Error GoTo 0
If PvtTbl Is Nothing Then    
    ' create a new Pivot Table in "PivotTable1" sheet, start from Cell A3
    Set PvtTbl = PivotSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PivotSht.Range("A3"), TableName:="PivotTable1")   

     'Create the headings and row and column orientation
     'Sum and all other Pivot Table setting
Else
    ' just refresh the Pivot cache with the updated Range (data in "DATA" Sheet)
    PvtTbl.ChangePivotCache PvtCache
    PvtTbl.RefreshTable
End If


' ****** THIS SECTION RELATED DIRECTLY TO YOUR POST ******
Dim PvtFld              As PivotField    
Dim PvtFldSubTotalsOn   As Boolean

' loop through all Pivot Fiels in PiVot Table
For Each PvtFld In PvtTbl.PivotFields

    ' check if Pivot Field Name is "Region"
    If PvtFld.Name = "Region" Then
        ' call your function
        PvtFldSubTotalsOn = PivotFieldSubtotalsOn(PvtFld)
        ' for debug only
        MsgBox "Pivot Field " & PvtFld.Name & " value is " & PvtFldSubTotalsOn
    End If
Next PvtFld

End Sub

选项2

如果您希望“授权”您的功能,您可以在功能中执行所有“循环”和If,并只发送您要检查的PivotField的名称{是否{ {1}}已启用,直接发送SubTotal

String

Function PivotFieldSubtotalsOn(pvtFieldName As String) As Boolean Dim PvtFld As PivotField ' loop through all Pivot Fiels in Pivot Table For Each PvtFld In PvtTbl.PivotFields ' check if Pivot Field Name is "Region" If PvtFld.Name = pvtFieldName Then If PvtFld.Subtotals(1) = True Then PivotFieldSubtotalsOn = True Else PivotFieldSubtotalsOn = False End If Exit For End If Next PvtFld End Function 代码(用于测试此函数):

Sub