简体   繁体   中英

Pull up “Save As” box in VBA to save Chart and Range to PDF

I am trying to assign a Macro to a button that will save a plot and a few ranges of values to a single PDF file, that the user names (and chooses the save location for) themselves. So far, I can call open the save as box, from another question I saw answered here, but I'm unsure how to specify what exactly I want saved.

Sub Save()

Dim bFileSaveAs As Boolean
bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
If Not bFileSaveAs Then MsgBox "User cancelled", vbCritical
Dim chrt As Chart
Dim rng As Range

End Sub

I've attached a picture of the workbook, and circled the parts I want saved, for help in seeing what I'm going for. Thanks!

在此处输入图片说明

If you want to save the multiple ranges as PDF then you can try this code:

    Sub CreateMultiRangeOnePagePDF()

  Dim RangeArray() As Variant
  Dim x As Long, LR As Long
  Const RngPad As Long = 2 'set to number of rows between ranges
  
  RangeArray = Array("Sheet1!A1:D30", "Sheet1!E1:H30", "Sheet1!I1:L30")
  
  Application.ScreenUpdating = False
  
  Sheets.Add After:=Sheets(Sheets.Count)
  
  For x = 0 To UBound(RangeArray)
  
    LR = Sheets(Sheets.Count).Cells(Rows.Count, 1).End(xlUp).Row
    
    If LR <> 1 Then LR = LR + 1 + RngPad
    
    Range(RangeArray(x)).Copy
    
    Sheets(Sheets.Count).Cells(LR, 1).PasteSpecial Paste:=xlPasteValues
    
    Selection.PasteSpecial Paste:=xlPasteFormats
    
    Application.CutCopyMode = False
    
  Next x
  
  Sheets(Sheets.Count).ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="Z:\Test.pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    
  Application.DisplayAlerts = False
  Sheets(Sheets.Count).Delete
  Application.DisplayAlerts = True
  
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM