0

我构建了这段代码,它适用于前四个过滤项目。第五天它停止工作。它也拉错了名字。我刷新了 PIVOT 表,它显示“Ft Lauderdal, FL”,但是当 VBA 调试器关闭并且我将鼠标悬停在 piOffice.Name 上时,它显示“Ft Lauderdal, FL”。这是我修复之前的旧名称。我还尝试了不同的变体,因此没有空格(例如 Ft_Lauderdale,FL)。每次我仍然得到运行时错误代码 5 并且当我将鼠标悬停在 piOffice.Name 上时,它仍然显示“Ft Lauderdal, FL”。

Sub Deferred_Rent_To_PDF()

Dim strWorkbook As String
Dim strWorksheet As String
Dim strPivotTable As String
Dim pdfFilename As Variant
Dim strPivotFilter As String
Dim strDocName As String
Dim ptDeferredRent As pivotTable
Dim piOffice As PivotItem

strWorkbook = "Schedule of Leases - Beta"
strWorksheet = "Deferred"
strPivotTable = "DeferredRent"

Workbooks(strWorkbook).Activate
Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)

    For Each piOffice In ptDeferredRent.PageFields("Office").PivotItems
        ptDeferredRent.PageFields("Office").CurrentPage = piOffice.Name   '<---------- ISSUE IS HERE
        strPivotFilter = Worksheets(strWorksheet).Range("H1")
        strDocName = "Deferred Rent - " & strPivotFilter & " - " & Format(Date, "mm-dd-yy")
        pdfFilename = Application.GetSaveAsFilename(InitialFileName:=strDocName, _
            FileFilter:="PDF, *.pdf", Title:="Save As PDF")

            ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=pdfFilename, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=False, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False

    Next piOffice

End Sub
4

1 回答 1

1

您得到的原因Run Time error 5: Invalid procedure call or argument是因为PivotCache仍然保留“旧名称”,无论 PivotCache 是否已刷新。

在此处输入图像描述

为了解决此问题,您需要更改PivotCache.MissingItemsLimit 属性 (Excel)这是XlPivotTableMissingItems 枚举 (Excel)的有效值 。我建议将其更改为:xlMissingItemsNone通过添加此行:

ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone

在此行之后:

Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)

您修改后的代码将如下所示:

Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone
于 2020-01-24T18:11:38.963 回答