在此Excel VBA代码中,如何指定仅包括Sheet1中的A1:M500,而其他内容都相同?

aydmsdu9  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(127)
Sub SavetoPDF()
    Application.ScreenUpdating = False
    Dim UseName As Variant
    Dim printRanges As Range
     
    UseName = Application.GetSaveAsFilename( _
        InitialFileName:="Report.pdf", _
        FileFilter:="PDF files, *.pdf", _
        Title:="Export to pdf")
         
   Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=UseName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
    
End Sub
mbzjlibv

mbzjlibv1#

保存为PDF

快速修复

Sub SavetoPDF()
    
    Const PrintAreaAddress As String = "A1:M500"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    
    ' Store print area in a variable.
    Dim SavedPrintArea As String: SavedPrintArea = ws.PageSetup.PrintArea
    ' Change the print area to the required one.
    ws.PageSetup.PrintArea = PrintAreaAddress
    
    Application.ScreenUpdating = False
    
    Dim UseName As Variant
     
    UseName = Application.GetSaveAsFilename( _
        InitialFileName:="Report.pdf", _
        FileFilter:="PDF files, *.pdf", _
        Title:="Export to pdf")
         
    wb.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=UseName, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    
    ' Restore previous print area.
    ws.PageSetup.PrintArea = SavedPrintArea
    ws.Select
    
    Application.ScreenUpdating = True

End Sub

相关问题