0

我正在尝试将宏分配给一个按钮,该按钮会将绘图和一些值范围保存到单个 PDF 文件中,用户自己命名(并为其选择保存位置)。到目前为止,我可以从我在这里看到的另一个问题中调用打开另存为框,但我不确定如何指定我想要保存的确切内容。

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

我附上了工作簿的图片,并圈出了我想要保存的部分,以帮助了解我的目标。谢谢!

在此处输入图像描述

4

1 回答 1

0

如果要将多个范围保存为 PDF,则可以尝试以下代码:

    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
于 2021-06-17T17:23:05.217 回答