我有一个Excel VBA脚本,可以将工作表的内容导出为PDF。它在单个工作表中运行良好,但现在我被要求让它包含第二个工作表。但是,现在使用下面的脚本只包括第二个工作表而不是第一个工作表。任何人都可以建议为什么不包括该对的第一个工作表?
Dim sheetsToPrint as Variant(1)
sheetsToPrint(0) = FirstWorksheet.Name
sheetsToPrint(1) = SecondWorksheet.Name
FirstWorksheet.PageSetup.PrintArea = Union(FirstWorksheet.Range("B2:I10"), FirstWorksheet.UsedRange).Address
With FirstWorksheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -4
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
End With
SecondWorksheet.PageSetup.PrintArea = Union(secondPage.Range("B2:I10"), secondPage.UsedRange).Address
With SecondWorksheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -4
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
End With
saveAndClose sheetsToPrint
saveAndClose函数的定义如下:
Private Sub saveAndClose(ByRef sheetsToPrint() As Variant)
Sheets(sheetsToPrint).Select
Dim primarySheetName As String
primarySheetName = CStr(sheetsToPrint(0))
Application.DisplayAlerts = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputDirectoryPath & ":" & primarySheetName & ".pdf"
Sheets(sheetsToPrint).Delete
Application.DisplayAlerts = True
End Sub
这是批处理的一部分,所以我还没有复制我的所有代码,但这是当前导出PDF的部分,该PDF仅包含来自sheetsToPrint(1)但不包含sheetToPrint(0)的信息。我整个下午一直在敲打这个问题,因为据我所知,这与互联网上关于如何将多张纸张导出为PDF的每个例子相同。
有人能发现我出错的地方吗?
答案 0 :(得分:0)
你可以这样设置:
Dim sheetsToPrint As Sheets
Set sheetsToPrint = Sheets(Array("Blad1", "Blad2", "Blad3"))
然后在saveandclose sub
中使用sheetsToPrint.select