我试图以PDF格式打印多个工作表。下面是代码,但它给下标超出范围的错误。请帮助将多个页面打印为PDF
PdfFile = ActiveWorkbook.FullName
Worksheets("Report").PageSetup.Orientation = xlLandscape
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = "SPC Report " & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf" 'PdfFile & "_" & Worksheets("Report").name & ".pdf"
' Export activesheet as PDF
With Worksheets("Report")
ThisWorkbook.Sheets(ShtNames).Select
.ExportAsFixedFormat Type:=xlTypePDF, Filename:="SPC Report " & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
ThisWorkbook.Sheets("Report").Select
End With
这里是我声明ShtNames并为其赋值的地方。
Dim ShtNames() As String
ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count/2 + 1)
i = Sheets.Count
j = 1
Do While i > Sheets.Count/2
ShtNames(j) = Sheets(i).Name
i = i - 1
j = j + 1
Loop
我已经编辑您的回复的问题。另外,ShtNames中的单个数组变量是正确的,ShtNames(1),ShtNames(2)都可以正常工作,但无法在单个select语句中选择给定的表单。 –
https://stackoverflow.com/questions/14404650/save-multiple-sheets-to-pdf – Luuklag
可能重复[保存多张PDF到.pdf](https://stackoverflow.com/questions/14404650/save-multiple -sheets-to-pdf) – Wolfie