Sub PrintWorksheetsToPDF()
Dim ws As Worksheet Dim MySheets As String Dim SaveLocation As String
'Prompt user to select the worksheets to print MySheets = InputBox("Enter the worksheets to print separated by a comma", "Worksheets to Print")
'Prompt user to select the save location With Application.FileDialog(msoFileDialogSaveAs) .Title = "Select location to save PDF files" .Filters.Clear .Filters.Add "PDF Files", "*.pdf" .FilterIndex = 1 'PDF files filter .InitialFileName = "myfile.pdf" If .Show = True Then SaveLocation = .SelectedItems(1) Else Exit Sub End If End With
'Loop through selected worksheets and print each worksheet to PDF For Each ws In ActiveWorkbook.Worksheets If InStr(MySheets, ws.Name) > 0 Then ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SaveLocation, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End If Next ws
End Sub
这个代码将弹出一个输入框,让用户输入要打印的工作表名称,然后弹出一个文件保存对话框,让用户选择将PDF文件保存到何处。然后,循环遍历每个选择的工作表,并将其打印到PDF文件中。
Sub PrintWorksheetsToExcel()
Dim ws As Worksheet Dim MySheets As String Dim SaveLocation As String
'Prompt user to select the worksheets to print MySheets = InputBox("Enter the worksheets to print separated by a comma", "Worksheets to Print")
'Prompt user to select the save location With Application.FileDialog(msoFileDialogSaveAs) .Title = "Select location to save Excel files" .Filters.Clear .Filters.Add "Excel Files", "*.xlsx" .FilterIndex = 1 'Excel files filter .InitialFileName = "myfile.xlsx" If .Show = True Then SaveLocation = .SelectedItems(1) Else Exit Sub End If End With
'Loop
上一篇:遍历数据网格中的行