【问题标题】:VBA Pivot Item.Name Pulling Wrong ValueVBA Pivot Item.Name 提取错误值
【发布时间】:2020-05-11 01:16:03
【问题描述】:

我构建了这段代码,它适用于前四个过滤项。第五天它停止工作。它也拉错了名字。我刷新了 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

【问题讨论】:

  • 注意到除非单元格H1随着页面的变化而改变,否则每次运行的PDF文件的名称都是相同的。
  • 是的,H1 是过滤器单元格,每次都会更改城市名称。 :)
  • 您是否应用了建议的更改?
  • 我在看到这个之前就下班了,所以我打算在星期一早上试试。

标签: excel vba pivot-table pivotitem


【解决方案1】:

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

为了解决这个问题,您需要更改PivotCache.MissingItemsLimit property (Excel)。这是有效值 XlPivotTableMissingItems enumeration (Excel)。我建议通过添加以下行将其更改为:xlMissingItemsNone

ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone

紧接着这一行:

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

您修改后的代码将是这样的:

Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多