使用VBA在多个Excel工作表中从不同范围创建PDF [英] Create PDF from different ranges in multiple Excel sheets with VBA

查看:153
本文介绍了使用VBA在多个Excel工作表中从不同范围创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将几张Excel工作表导出到一个PDF文件中.我当前正在使用以下内容:

I need to export several Excel sheets into one PDF file. I am currently using the following:

Sub CreatePDF()

    Sheets("ReportPage1").Select
    Range("Print_Area").Select
    Sheets("ReportPage2").Select
    Range("Print_Area").Select
    Sheets("ReportPage3").Select
    Range("Print_Area").Select

        ThisWorkbook.Sheets(Array("ReportPage1", "ReportPage2", "ReportPage3")).Select
        Selection.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            FileName:="C:\temp\temp.pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True

    Sheets("ReportPage1").Select
    Range("A1").Select

End Sub

问题是所有打印区域都不同.所生成的PDF使用的是所有页面的ReportPage1打印区域的范围地址.

The problem is that all print areas are different. The PDF that is generated is using the range address of print area from ReportPage1 for all the pages.

我在VBA之外的Excel本身中进行了尝试,例如,当我选择ReportPage1!A1:E30和ReportPage2!A1:F70时,当我选择两个工作表时,它将选择的范围更改为ReportPage1!A1: E30和ReportPage2!A1:E30.

I tried in Excel itself, outside of VBA, and when I have selected, for example, ReportPage1!A1:E30 and ReportPage2!A1:F70, when I select both sheets it will change the selected ranges to ReportPage1!A1:E30 and ReportPage2!A1:E30.

有什么办法解决这个问题吗?

Any ideas how to solve this?

任何帮助将不胜感激.

推荐答案

好,我解决了.如果我在特定页面上未选择任何范围,它将自动获取每张纸的Print_Area范围.

Ok, I solved it. If I don't select any ranges on the specific pages it will automatically get the Print_Area range of each sheet.

Sub CreatePDF()

    ThisWorkbook.Sheets(Array("ReportPage1", "ReportPage2", "ReportPage3")).Select

    Sheets("ReportPage1").Activate

    ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            FileName:="C:\temp\temp.pdf", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True

    Sheets("ReportPage1").Select
    Range("A1").Select

End Sub

这篇关于使用VBA在多个Excel工作表中从不同范围创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆