将动态轴设置为打印区域

时间:2017-10-26 14:52:41

标签: excel vba excel-vba dynamic

我已尝试通过命名范围,但由于某种原因,在运行宏之后,等式在指定范围内自动更改。所以我试图在宏中设置范围。 我正在编写一个宏,它将通过切片器查看并将2张纸保存为PDF。第二个选项卡具有枢轴,每次切片器更改时,区域范围将更改。有人可以帮助我,以便动态打印到PDF吗?

Sub Loopexport()

Dim ws2 As Worksheet
Dim PT As PivotTable

'Hide non-printable sheets
    Sheets("Overview").Visible = False
    Sheets("KPExport").Visible = False

Set ws2 = Sheets("Id CUps")

Set PT = ws2.PivotTables(1)

'Set print areas
    Sheets("Stats").PageSetup.PrintArea = "$A$1:$M$39"
    ws2.PageSetup.PrintArea = PT.TableRange2 'this is where I am receiving the type mismatch. and the = "My named range" kept changing automatically at the end.

    With Sheets("Id CUps").Columns("D:D")
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
    End With

'Print Regions
'Export N1
ActiveWorkbook.SlicerCaches("Slicer_Dt").VisibleSlicerItemsList = Array("[UserID].[Dept].&[N1]")

ChDir ("C:\My Docs\A\B\Export")
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\My Docs\A\B\Export\N1 - " & Format(Now(), "YYYY") & "M" & Format(Now(), "MM")

End Sub

1 个答案:

答案 0 :(得分:1)

ws2.PageSetup.PrintArea = PT.TableRange2.Address