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
Excel 参考数据透视表过滤器_Excel_Vba_Filter_Reference_Pivot Table - Fatal编程技术网

Excel 参考数据透视表过滤器

Excel 参考数据透视表过滤器,excel,vba,filter,reference,pivot-table,Excel,Vba,Filter,Reference,Pivot Table,我正在尝试引用一个透视表过滤器作为生成PDF的另存为名称。我似乎找不到任何PIVOT表对象引用的组合来执行此操作 Sub Deferred_Rent_To_PDF() Dim strWorksheet As String Dim strPivotTable As String Dim pdfFilename As Variant Dim strDocName As String Dim ptDeferredRent As pivotTable strWorksheet = "Deferred

我正在尝试引用一个透视表过滤器作为生成PDF的另存为名称。我似乎找不到任何PIVOT表对象引用的组合来执行此操作

Sub Deferred_Rent_To_PDF()

Dim strWorksheet As String
Dim strPivotTable As String
Dim pdfFilename As Variant
Dim strDocName As String
Dim ptDeferredRent As pivotTable

strWorksheet = "Deferred"
strPivotTable = "DeferredRent" 

Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
'strDocName = ptDeferredRent.                  <----- THIS IS WHERE I NEED HELP

pdfFilename = Application.GetSaveAsFilename(InitialFileName:=strDocName, _
    FileFilter:="PDF, *.pdf", Title:="Save As PDF")

    If pdfFilename <> False Then

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

End Sub
Sub-Deferred\u Rent\u To\u PDF()
将工作表尺寸标注为字符串
Dim strPivotTable作为字符串
Dim PdfileName作为变体
Dim strDocName作为字符串
Dim ptDeferredRent作为数据透视表
strWorksheet=“延期”
strPivotTable=“延期租金”
Set ptDeferredRent=工作表(strWorksheet)。数据透视表(strPivotTable)

'strDocName=ptDeferredRent 试着这样做:

您正在查找
字段\u name.CurrentPage
,但仅当
字段\u name.Orientation=xlPageField
(即筛选字段,而不是行、数据或列字段或隐藏字段)时才可以查找

Sub-Deferred\u Rent\u To\u PDF()
将工作表尺寸标注为字符串
Dim strPivotTable作为字符串
Dim PdfileName作为变体
Dim strDocName作为字符串
Dim ptDeferredRent作为数据透视表
strWorksheet=“Pivot(2)”“延期”
strPivotTable=“数据透视表7”'“递延租金”
此工作簿.Sheets(strWorksheet).激活
Set ptDeferredRent=工作表(strWorksheet)。数据透视表(strPivotTable)

'strDocName=ptDeferredRent。非常感谢。你能提供一些关于逻辑的评论吗。看起来您每次都在重置格式?如果您想查看,请查看更完整(有效)的答案和指向文件的链接。嘿,Doupis,我更新的答案和指向Excel文件的链接是否回答了您的问题?如果是这样,请考虑将投票和标记作为正确答案。谢谢
Sub Deferred_Rent_To_PDF()

Dim strWorksheet As String
Dim strPivotTable As String
Dim pdfFilename As Variant
Dim strDocName As String
Dim ptDeferredRent As PivotTable

strWorksheet = "Pivot (2)" '"Deferred"
strPivotTable = "PivotTable7" '"DeferredRent"

ThisWorkbook.Sheets(strWorksheet).Activate

Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
'strDocName = ptDeferredRent.                  <----- THIS IS WHERE I NEED HELP

strDocName = Get_Pivot_filter_field(ptDeferredRent)

If strDocName <> "not found" Then
    Debug.Print strDocName

    pdfFilename = Application.GetSaveAsFilename(InitialFileName:=strDocName, _
        FileFilter:="PDF, *.pdf", Title:="Save As PDF")

        If pdfFilename <> False Then

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

End Sub

Function Get_Pivot_filter_field(pvt As PivotTable)

    'On Error Resume Next

    Debug.Print pvt.Name
    Pivot_Table_Name = pvt.Name
    Debug.Print pvt.PivotFields.Count

    Get_Pivot_filter_field = "not found"

    For Each field_name In pvt.VisibleFields 'pvt.PivotFields
        If pivot_field_active(Pivot_Table_Name, field_name) Then

            With field_name

                Debug.Print field_name & " " & .Orientation

                If .Orientation = xlPageField Then 'xlDataField (4)' 'xlColumnField (2)' 'xlHidden (0)' 'xlPageField (3)' 'xlRowField (1)'

                    Debug.Print field_name & " " & .Orientation & .CurrentPage
                    Get_Pivot_filter_field = .CurrentPage

                Else
                    Debug.Print field_name & " not filter field"
                End If
            End With
        Else
            Debug.Print field_name & " not active"
        End If
    Next

End Function

Function pivot_field_active(ByVal Pivot_Table_Name As String, ByVal strName As String) As Boolean

    Dim strTemp As String
    On Error Resume Next
    With ActiveSheet.PivotTables(Pivot_Table_Name).PivotFields(strName)
        If .NumberFormat = "$#,##0" Then
            'Do nothing no error
        End If
        If .CurrentPage Then
            Err.Clear
        End If
    End With
    If Err = 0 Then pivot_field_active = True Else pivot_field_active = False

End Function