使用excel中的button将数据范围保存为PDF

我有一个代码,将popup一个保存对话框,当我的Excel表单中的一个button被点击:

Sub SavePDF() Dim X X = Application.GetSaveAsFilename(InitialFileName:=Range("F8") & "_" & Range("F6"), _ FileFilter:="PDF files, *.pdf", _ Title:="Save PDF File") If TypeName(X) = "Boolean" Then Else ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=X, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If End Sub 

…我需要的是添加一些东西,这将只保存点击button时的一定范围的数据。 我的范围是:B2到J44。 当我点击保存button的时候,它保存了我不想要的整张纸。

提前致谢。

尝试以下。

 Option Explicit Sub CreatePDF() Dim wSheet As Worksheet Dim vFile As Variant Dim sFile As String Set wSheet = ActiveSheet sFile = Replace(Replace(wSheet.Name, " ", ""), ".", "_") _ & "_" _ & Format(Now(), "yyyymmdd\_hhmm") _ & ".pdf" sFile = ThisWorkbook.Path & "\" & sFile vFile = Application.GetSaveAsFilename _ (InitialFileName:=sFile, _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Folder and FileName to save") If vFile <> "False" Then wSheet.Range("B2:J44").ExportAsFixedFormat _ Type:=xlTypePDF, _ FileName:=vFile, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False MsgBox "PDF file has been created." End If End Sub 

看到这里的例子


文件保存为SheetName_TodaysDate_HoursMinutes

如果你想添加秒,只需在_hhmmss旁边添加ss