【问题标题】:Loop Through Power Pivot, Pivot Table Filter and Print to PDF循环通过 Power Pivot、数据透视表过滤器并打印到 PDF
【发布时间】:2019-12-04 00:51:50
【问题描述】:

目标是循环遍历 Excel Power Pivot、数据透视表并将每个过滤结果打印到特定文件位置的 PDF 中。

当代码到达输出的 for 循环时,它给了我一个错误

“运行时错误 - 438 对象不支持该属性或方法”

上线For Each pi In pt

Sub Button1_Click()
 Dim strPath As String
 Dim wksSource As Worksheet
 Dim pt As PivotTable
 Dim pf As PivotField
 Dim pi As PivotItem
 Dim cf As CubeField

 Set wksSource = Worksheets("Summary for Each Analyst")

 Set pt = wksSource.PivotTables("PivotTable1")

 Set cf = pt.CubeFields("[Std_MainData].[CredentialingAnalyst]")

 If cf.Orientation <> xlPageField Then
  MsgBox "There's no 'Credentialing Analyst' field in the Report Filter. Try again!", vbExclamation
 End If

 strPath = "H:\"

 If Right(strPath, 1) <> "\" Then strPath = strPath & "\"

  ActiveWorkbook.ShowPivotTableFieldList = False

  pt.PivotCache.Refresh

  For Each pi In pt
   wksSource.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & pi.Name & ".pdf"
  Next pi

【问题讨论】:

  • 有人有什么想法吗?
  • pi 属于 pf,不属于 pt。你不能说“pi in pt”。必须是pt.pf中的"pi。例如:Worksheets("sheet1").PivotTables(1).PivotFields("year").PivotItems("1998").Visible = False
  • 早上好,可以评论为代码吗?我尝试将我的代码更改为“pt.pf 中的 pi”,它给了我与上面写的相同的错误。请记住,这是一个 Power Pivot,因此它是基于多维数据集的,我不知道这是否会产生影响,或者不循环遍历该多维数据集的数据透视表。 @RADO

标签: excel vba for-loop pivot-table powerpivot


【解决方案1】:

请将 pt 更改为 pv,正如 RADO 指出的那样:

For Each pv In pt.PivotFields
   wksSource.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & pv.Name & ".pdf"
Next pv

谢谢

【讨论】:

  • 早上好,您将 pv 定义为什么?我尝试将您的代码复制并粘贴到工作簿中,但没有用。然后我将定义的 pv 更改为 Pivot 项,将 pv 调暗为 PivotItem,但这不起作用。
  • 也感谢您的回复,只是想指出这是一个 CubeField Power Pivot,数据透视表。
  • 您能否提供示例数据以便我重现该问题?
【解决方案2】:

整个周末我都在为同样的问题苦苦挣扎,终于通过切片器而不是报告过滤器循环解决了这个问题。

'This VBA will loop through your Power Pivot slicer and print the results to PDF.
'To get it working change slicer name and storage location in below VBA.

Private Sub PowerPivotLoopSlicerPrintPDF()
Dim SC As SlicerCache
Dim SL As SlicerCacheLevel
Dim SI As SlicerItem

Set SC = ActiveWorkbook.SlicerCaches("Slicer_Kolonne1") 'Add slicer name between " "
Set SL = SC.SlicerCacheLevels(1)

'c(ounter) is set to 1, ready to begin
c = 1


'Repeat the a loop until number of prints exceeds number of items in slicer
Do While c <= SC.SlicerCacheLevels.Item.Count + 1

'This makes sure that SI is the correct slicer. Needed for corrent file name.
    For Each SI In SL.SlicerItems
        If SI.Selected = True Then
        SlicerverdiIndex = c
    Exit For
        End If
    Next SI


    'PRINT CODE
    Dim FName           As String
    Dim FPath           As String

    'Define file path for printed file storage
    FPath = "C:\Users\remia\Desktop\VBA\"   'Choose your filepath
    FName = SI.SourceName

    'Define WHAT to print and how to build file name
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    FPath & "\" & FName & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    False

    'PRINT CODE FINISHED

'Sets the slicer to the last item in the list
If SlicerverdiIndex = 1 Then
    SlicerverdiIndex = SC.SlicerCacheLevels.Item.Count + 1
End If
SC.VisibleSlicerItemsList = SL.SlicerItems(SlicerverdiIndex - 1).Name

'Adds 1 to the counter, will loop until end of slicer has been reached.
c = c + 1

Loop

End Sub

【讨论】:

  • 这太棒了!非常感谢,但是我遇到了一个问题,第一行 Set SC = ActiveWorkbook.SlicerCaches("Slicer_Kolonne1") 在我的工作簿中提供了一个错误。我想我在定位过滤器时遇到问题,它称为凭证分析师,它位于 ("[Std_MainData].[CredentialingAnalyst]")
  • 将要过滤的列添加为工作簿中的切片器。右键单击切片器,切片器设置,然后你会看到“在公式中使用的名称”。这就是 SC 行的目标名称 :)
  • 这完美!非常感谢!我添加并支持并验证了您的帖子! @fenixen
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多