Home > other >  In this Excel VBA code, how would I specify to only include A1:M500 from Sheet1 all else being the s
In this Excel VBA code, how would I specify to only include A1:M500 from Sheet1 all else being the s

Time:11-08

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

CodePudding user response:

Save to PDF

A Quick Fix

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
  • Related