【发布时间】:2020-05-02 08:33:15
【问题描述】:
因此,在下面的示例代码中,它可以通过指定工作表 # 在 Excel 中对特定工作表进行 PDF 处理,但是有没有办法通过工作表名称而不是数字来指定 PDF 范围?
Sub createPdf()
Dim SheetArr() As String
Dim i As Integer
Dim startSheet As Integer
Dim endSheet As Integer
startSheet = 1
endSheet = 2
Dim folderPath As String
folderPath = "C:\Users\xxxx\Desktop\Pdfs" 'change to your
user.
MkDir (folderPath)
For Each ws In ThisWorkbook.Worksheets 'This statement
starts the loop
If ws.Index >= startSheet And ws.Index <= endSheet Then ' <>
"Sheet3" Then
ReDim Preserve SheetArr(i)
SheetArr(i) = ws.Name
i = i + 1
Debug.Print (ws.Name)
End If
Next
Sheets(SheetArr).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
Filename:=folderPath & "\Sales", _
openafterpublish:=False, ignoreprintareas:=False
MsgBox "All done with pdf's"
End Sub
【问题讨论】:
-
这能回答你的问题吗? Save multiple sheets to .pdf
-
嗨,谢谢您的输入,但不完全是,因为这是按名称指定工作表以供选择,而不是按名称指定工作表作为范围的两端。
-
好吧,我误解了你的问题。
-
每个工作表是否有不同的范围,您想打印到 .pdf?是否有您不想打印的工作表?您是否尝试仅打印工作簿中的一个特定工作表?
-
是的,因此 pdf 的页数根据 PDF 开始和结束选项卡的位置是动态的。