我想通过vba从PowerPivot数据创建数据透视表。
我已经通过vba代码将年和月放在行字段中,现在我想根据用户选择的特定条件过滤掉我的数据;但这不允许我将行字段中特定字段项目的visible属性设置为True或false
Sub Power_Pivot()
Dim cn As WorkbookConnection
Dim pc As PivotCache
Dim pt As PivotTable
Dim cf As CubeField
Dim pf As PivotField
Dim pi As PivotItem
Dim coll As New Collection
Range("V:Z").Delete
Set cn = ThisWorkbook.Connections("SqlServer sql1 bio_app")
Set pc = ThisWorkbook.PivotCaches.Create(xlExternal, cn, xlPivotTableVersion15)
Set pt = pc.CreatePivotTable(Tool.Range("V3"), "parameter_pivot")
Set cf = pt.CubeFields("[Query].[Postyear]")
cf.Orientation = xlRowField
cf.Position = 1
Set cf = Nothing
Set cf = pt.CubeFields("[Query].[postmonth]")
cf.Orientation = xlRowField
cf.Position = 2
Set cf = pt.CubeFields.GetMeasure("[Query].[itemsalesamt]", xlSum)
pt.AddDataField cf, "Total Sum"
For i = 2 To 5 Step 1
If Range("H" & i).Value = "Yes" Then
Show_ = Range("G" & i).Value
cf.PivotFields("postyear").PivotItems(Show_).Visible = True
Else
Show_ = Range("G" & i).Value
cf.PivotFields("postyear").PivotItems(Show_).Visible = False
End If
Next i
End Sub
我想过滤出Year Years字段中的数据,我只想显示2016和2019可见,而2017和2018隐藏。