Print range with VBA

  • Presently using the following code:

    Code
    Sub PDF_laptop()
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            "C:\Users\cyril\Desktop\" & Range("x1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            False
    End Sub

    The above works with a Set Print Area however I would like to add one VBA to export as PDF the range such as ("AD16:AT71")
    same filename, same quality...
    Thank you for any advice.

  • Try this


    Code
    Sub PDF_laptop()
    ActiveSheet.PageSetup.PrintArea = "AD16:AT71"
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
     "C:\Users\cyril\Desktop\" & Range("x1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
     False
    End Sub
  • Thank you.

    Had to create a new one with a different range since the vba .PrintArea would register what is stated in the Sub PDF_Laptop.
    Thank you for your help, much appreciated.

    :thumbup:

    (how do you mark a thread as resolved?)

  • Pleased to help.


    Post back if you need further help.


    Visit my web site, http://www.excel-it.com, for more examples and some helpful articles.


    I've marked the question as resolved.

  • Just for the heck of it, instead of having 2 macros, use a single one.

    Change references as required.

    Code
    Sub Maybe()
    Dim prntRng, i As Long
    prntRng = Array("A1:I20", "A1:I40")    '<---- Change as required
    For i = LBound(prntRng) To UBound(prntRng)
    ActiveSheet.PageSetup.PrintArea = Range(prntRng(i)).Address
    ActiveSheet.PrintOut , , , , , True, , "C:\Users\cyril\Desktop\" & Range("X1").Value & i & ".PDF"
    Next i
    End Sub
  • ...Just for the heck of it, instead of having 2 macros, use a single one...

    Hi, thanks for the added code, actually I need to be able to choose which range I need based on the selected slicers. Not every query requires the added info of the second range.
    However this may be useful for maybe other projects. Thanks again.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!