繁体   English   中英

将多个工作表另存为PDF

[英]Save multiple worksheets as PDF

我对使用VBA相当满意,这是我第一次在该论坛中发布问题,所以如果我违反任何发布规则,请多多包涵。

下面是一段代码,用于将1个或多个工作表从Excel工作簿打印/保存到PDF。 如果我要导出的每个工作表中的数据都适合单个页面,则效果很好。 我发现的问题是,如果我的第一张工作表中的数据范围大于1页,则仅第一张工作表将被导出,并且只能导出到第一页。 关于如何解决此问题的任何建议或建议?

'Ask user if report needs to be saved as a pdf
PdfCheck = MsgBox("Save report as PDF", vbYesNo, "Save PDF") 
'use result of <PdfCheck> to control if statement
If PdfCheck = vbYes Then
    Pdfsheets = InputBox("How many worksheets would you like to include in PDF")

    'Prints the number of sheets entered by user
    'starts at first ACTIVE Worksheet to the result of <Pdfsheets>
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & _
     ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" & FileDate _
    , Quality:=xlQualityStandard, IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, From:=1, To:=Pdfsheets, OpenAfterPublish:=True
Else
    GoTo X
End If
X:
End Sub

我使用的方法是循环浏览工作簿中的工作表,然后选择符合我的条件的工作表。 然后,我使用ExportAsFixedFormat创建我的pdf。 请注意,您无法选择隐藏的工作表,因此,如果工作簿包含这些工作表,则需要添加if / then来检查可见性。

Pdfsheets = InputBox("How many worksheets would you like to include in PDF")
    for x=1 to Pdfsheets
        thisworkbook.sheets(x).select (false) 'use false to select multiple sheets
    next x

'Prints the number of sheets entered by user--starts at first ACTIVE   
Worksheet to the result of <Pdfsheets>
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" &    FileDate _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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