VBA 宏可将选定的工作表导出到单独的 pdf 文件中

问题描述 投票:0回答:1

我需要将工作簿中选定的工作表导出为单独的 pdf 文件。但下面的代码并没有完全做到这一点,它的作用是将整个选择的工作表导出到一个 pdf 中,并使用不同的工作表名称复制相同的 pdf。

有什么建议可以让代码将每个选定的工作表导出为单独的 pdf 吗?

Sub ExportAsPDF()

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Folder path"
If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With

If Folder_Path = "" Then Exit Sub



Dim sh As Worksheet

For Each sh In ActiveWindow.SelectedSheets

sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & Range("RD").Text & " " & sh.Name & " 2024.pdf"
Next

MsgBox "Done"

End Sub
excel vba pdf export
1个回答
0
投票
Option Explicit

Sub ExportAsPDF()

    Dim Folder_Path As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder path"
        If .Show = -1 Then Folder_Path = .SelectedItems(1)
    End With
    If Folder_Path = "" Then Exit Sub
    
    Dim sh As Worksheet, n As Long
    For Each sh In ActiveWindow.SelectedSheets
        sh.Activate
        sh.Select
        ActiveSheet.ExportAsFixedFormat xlTypePDF, _
        Folder_Path & Application.PathSeparator & Range("RD").Text & " " & sh.Name & " 2024.pdf", _
        IgnorePrintAreas:=True
        n = n + 1
    Next
    MsgBox n & " Sheets Exported"

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.