我正在尝试以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
答案 0 :(得分:0)
OP的初始问题似乎已经解决了。关于如何反向打印纸张的问题的更新答案现在包含在答案的底部
尝试在声明j = 0
的代码中设置ShtNames
并为其指定值:
我没有在代码顶部看到任何索引规范,因此我假设您的数组都是0索引。
您的Do While
循环使用计数器j
来访问ShtNames
- 数组中的元素,但是,在我看来,您首先要访问该数组的第二个元素(As ShtNames(0)
是第一个元素,ShtNames(1)
是第二个元素。)
我怀疑当你尝试将最后一个工作表名称分配给ShtNames
- 数组的最后一个索引时,你的索引1大于ReDim
& #39; d最初。
要以相反的顺序打印图纸,请按相反顺序分配ShtNames
:
Dim ShtNames() As String
ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count / 2 + 1)
i = Sheets.Count
j = UBound(ShtNames)
Do While i > Sheets.Count / 2
ShtNames(j) = Sheets(i).Name
i = i - 1
j = j - 1
Loop