动态合并多个打印区域以在Excel宏中保存为pdf

时间:2019-06-30 18:16:53

标签: excel vba

动态合并Excel工作表上的几个打印区域除了用于打印区域11到14的并集线以外,所有工作方式都很完美,如果我注释掉它们,其他所有内容都可以工作。在代码之间使用if语句来设置打印区域Page_one和11-14是否存在问题?

我得到的错误是运行时错误'1004':对象'_Global'的方法'Range'失败。

Sub SavetoPDF()

    Dim UseName As Variant
    Dim printRanges As Range

    UseName = Application.GetSaveAsFilename( _
        InitialFileName:="Analytics for Labs.pdf", _
        FileFilter:="PDF files, *.pdf", _
        Title:="Export to pdf")

    If UseName <> False Then

       Set printRanges = Range("Cover_Page")

        With Application.WorksheetFunction

           Set printRanges = Union(printRanges, Range("Page_one"))

           If .CountBlank(ActiveSheet.Range("b31")) = 0 Then Set printRanges = Union(printRanges, Range("Page_three"))
           If .CountBlank(ActiveSheet.Range("j31")) = 0 Then Set printRanges = Union(printRanges, Range("Page_four"))
           If .CountBlank(ActiveSheet.Range("b55")) = 0 Then Set printRanges = Union(printRanges, Range("Page_five"))
           If .CountBlank(ActiveSheet.Range("j55")) = 0 Then Set printRanges = Union(printRanges, Range("Page_six"))
           If .CountBlank(ActiveSheet.Range("b78")) = 0 Then Set printRanges = Union(printRanges, Range("Page_seven"))
           If .CountBlank(ActiveSheet.Range("j78")) = 0 Then Set printRanges = Union(printRanges, Range("Page_eight"))
           If .CountBlank(ActiveSheet.Range("b86")) = 0 Then Set printRanges = Union(printRanges, Range("Page_nine"))
           If .CountBlank(ActiveSheet.Range("j86")) = 0 Then Set printRanges = Union(printRanges, Range("Page_ten"))

           Set printRanges = Union(printRanges, Range("Print_eleven"))
           Set printRanges = Union(printRanges, Range("Print_twelve"))
           Set printRanges = Union(printRanges, Range("Print_thirteen"))
           Set printRanges = Union(printRanges, Range("Print_fourteen"))

        End With

        printRanges.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:=UseName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
    End If

End Sub

0 个答案:

没有答案