Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA透视项。名称拉错值_Excel_Vba_Pivot Table_Pivotitem - Fatal编程技术网

Excel VBA透视项。名称拉错值

Excel VBA透视项。名称拉错值,excel,vba,pivot-table,pivotitem,Excel,Vba,Pivot Table,Pivotitem,我构建了这段代码,它适用于前四个过滤项。在第五天它停止工作。它还取错了名字。我已经刷新了透视表,它显示“Ft Lauderdal,FL”,但当VBA调试器关闭时,我将鼠标悬停在piOffice上。名称它显示“Ft Lauderdal,FL”。这是我修好它之前的旧名字。我还尝试了不同的变体,因此没有空格(例如佛罗里达州劳德代尔堡)。每次我仍然会得到运行时错误代码5,当我将鼠标悬停在piOffice上时,它仍然显示“Ft Lauderdal,FL” Sub Deferred_Rent_To_PDF(

我构建了这段代码,它适用于前四个过滤项。在第五天它停止工作。它还取错了名字。我已经刷新了透视表,它显示“Ft Lauderdal,FL”,但当VBA调试器关闭时,我将鼠标悬停在piOffice上。名称它显示“Ft Lauderdal,FL”。这是我修好它之前的旧名字。我还尝试了不同的变体,因此没有空格(例如佛罗里达州劳德代尔堡)。每次我仍然会得到运行时错误代码5,当我将鼠标悬停在piOffice上时,它仍然显示“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
Sub-Deferred\u Rent\u To\u PDF()
作为字符串的Dim strWorkbook
将工作表尺寸标注为字符串
Dim strPivotTable作为字符串
Dim PdfileName作为变体
Dim strPivotFilter作为字符串
Dim strDocName作为字符串
Dim ptDeferredRent作为数据透视表
将办公室设置为数据透视项
strWorkbook=“租赁计划-测试版”
strWorksheet=“延期”
strPivotTable=“延期租金”
工作簿(strWorkbook)。激活
Set ptDeferredRent=工作表(strWorksheet)。数据透视表(strPivotTable)
对于ptDeferredRent.PageFields(“Office”).PivotItems中的每个piOffice

ptDeferredRent.PageFields(“Office”).CurrentPage=piOffice.Name'您获得运行时错误5的原因:无效的过程调用或参数
是因为无论数据透视缓存是否已刷新,数据透视缓存仍保留“旧名称”

为了解决此问题,您需要更改。这是有效值 . 我建议通过添加此行将其更改为:
xlmissingementsnone

ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone
Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
就在这一行之后:

ptDeferredRent.PivotCache.MissingItemsLimit = xlMissingItemsNone
Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
修改后的代码如下所示:

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

请注意,除非单元格
H1
随页面更改而更改,否则每次运行PDF文件的名称都是相同的。是的,H1是每次使用城市名称更改的过滤单元格。:)你申请了提议的更改吗?我在看到这个之前就离开了工作,所以我打算在周一早上试试。