Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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/16.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 为每个透视过滤器创建PDF_Excel_Vba_Pdf_Pivot - Fatal编程技术网

Excel 为每个透视过滤器创建PDF

Excel 为每个透视过滤器创建PDF,excel,vba,pdf,pivot,Excel,Vba,Pdf,Pivot,我目前有一个透视表,它创建了一个未付发票的对账单,并按帐户名进行筛选。我想创建一个宏,excel在其中为每个帐户生成PDF语句 我没有VBA的知识,所以我通过搜索google获得了下面的代码,并为我的pivot表编辑了它 但是,当我尝试运行它时,excel不喜欢这一行“.CurrentPage=pi.Name”。当我把它取出来时,它确实运行并创建了几个具有正确帐户名的PDF,但所有语句都是相同的 有人能帮忙吗 Option Explicit Sub test() Dim strPath As S

我目前有一个透视表,它创建了一个未付发票的对账单,并按帐户名进行筛选。我想创建一个宏,excel在其中为每个帐户生成PDF语句

我没有VBA的知识,所以我通过搜索google获得了下面的代码,并为我的pivot表编辑了它

但是,当我尝试运行它时,excel不喜欢这一行“.CurrentPage=pi.Name”。当我把它取出来时,它确实运行并创建了几个具有正确帐户名的PDF,但所有语句都是相同的

有人能帮忙吗

Option Explicit
Sub test()
Dim strPath As String
Dim wksSource As Worksheet
Dim PT As PivotTable
Dim pf As PivotField
Dim pi As PivotItem

Set wksSource = Worksheets("AP Pivot")

Set PT = wksSource.PivotTables("PivotTable1")

Set pf = PT.PivotFields("ACCOUNT")

If pf.Orientation <> xlPageField Then
MsgBox "There's no 'ACCOUNT' field in the Report Filter. Try again!", vbExclamation
Exit Sub
End If

strPath = "T:\"

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

ActiveWorkbook.ShowPivotTableFieldList = False

PT.PivotCache.Refresh

With pf
.ClearAllFilters
For Each pi In .PivotItems
.CurrentPage = pi.Name
wksSource.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & pi.Name & ".pdf"
Next pi
.ClearAllFilters
End With
End Sub
选项显式
子测试()
将strPath设置为字符串
将wksSource设置为工作表
数据透视表
Dim pf作为数据透视字段
Dim pi作为数据透视项
设置wksSource=工作表(“AP轴”)
Set PT=wksSource.PivotTables(“数据透视表1”)
设置pf=PT.数据透视字段(“账户”)
如果pf.Orientation xlPageField,则
MsgBox“报表筛选器中没有“帐户”字段。请重试!”,请使用感叹号
出口接头
如果结束
strPath=“T:\”
如果正确(strPath,1)“\”则strPath=strPath&“\”
ActiveWorkbook.ShowPivotTableFieldList=False
PT.PivotCache.Refresh
与pf
.ClearAllFilters
对于.PivotItems中的每个pi
.CurrentPage=pi.Name
wksSource.ExportAsFixedFormat类型:=xlTypePDF,文件名:=strPath&pi.Name&“.pdf”
下一个pi
.ClearAllFilters
以
端接头

您遇到的问题是,“Account”字段的pivot items集合中仍然包含数据中不再存在的值。因此,您的筛选器被设置为一个不可用的值。要解决此问题,请在
pt.PivotCache.MissingItemsLimit=xlMissingItemsNone
前面添加
pt.PivotCache.Refresh


这将从透视项目集合中删除所有缺少的项目。

行抛出的错误消息是什么?您可能遇到的一个问题是,“帐户”字段的透视项目集合中仍然包含数据中不再存在的值。当您在
pt.PivotCache.MissingItemsLimit=xlMissingItemsNone
之前添加pt.PivotCache.Refresh时,它是否有效?添加该行已完成,谢谢。