excel报告使用动态页面生成

时间:2017-12-04 16:09:54

标签: excel vba excel-vba

我正在尝试在excel中为公司的每个部门打印报告,以获得包含下面各部分的附录报告的单个报告。即人力资源司负责招聘,人员配置,合规,包容和多元化,领导力发展等部门。

我希望报告的静态页面后跟其后面的每个部门的四个动态页面。现在,我运行代码时只打印/生成一个部分。

Sub GenerateReports()
Dim intRowDiv as Integer
Dim intRowSec as Integer

intRowDiv = 13
intRowSec = 13

Do While Sheets("LOOKUP_VALUES").Cells(2,2).Value = Sheets("LOOKUP_VALUES").Cells(intRowDiv,1).Value

Application.Calculate

'file naming
tempFileName = Sheets("LOOKUP_VALUES").Cells(7,2).Value & " - CLIMATE.pdf"
tempFileName = Replace(tempFileName, "/", "_")
tempFileName = Replace(tempFileName, "\", "_")
tempFileName = Replace(tempFileName, "&", "_")
tempFileName = Replace(tempFileName, ":", "_")

'Select Static Pages
Sheets(Array("2018 CoverPage", "intro", "table of contents", "division snapshot", "engagement snapshot", "action planning", "resources", "branch TOC", "branch intro")).Select

'Select Dynamic Pages
Sheets(2018 CoverPage").Activate

Do While Sheets("LOOKUP_VALUES").Cells(intRowSec, 4).Value <> ""

Sheets("LOOKUP_VALUES").Cells(8,2).Value = Sheets("LOOKUP_VALUES").Cells(intRowSec,4).Value

Sheets(Array("2018 Page10", "2018 Page11", "2018 Page12", "2018 Page13")).Select

'print to local location on comp
tempFileName = "C:\Users\sslattery\Documents\clim_reports\reports\" & TempFileName

'print full
ActiveSheet.ExportAsFixedFormat Type:xlTypePDF, Filename:= _
tempFileName _
, Quality:=xlQUalityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

'next section
intRowSec = IntRowSec + 1
Loop

'next division
intRowDiv = intRowDiv + 1
Loop

End Sub

1 个答案:

答案 0 :(得分:0)

如果只打印一个部分,这意味着您的逻辑在某处被破坏。你有两个循环 - 一个内循环和一个外循环。确保每个都正确地循环使用这个简单的东西:

Sub GenerateReports()
    Dim intRowDiv As Integer
    Dim intRowSec As Integer

    intRowDiv = 13
    intRowSec = 13

    Do While Sheets("LOOKUP_VALUES").Cells(2, 2).Value = Sheets("LOOKUP_VALUES").Cells(intRowDiv, 1).Value
        Debug.Print "Loop A " & Sheets("LOOKUP_VALUES").Cells(2, 2).Value
        Do While Sheets("LOOKUP_VALUES").Cells(intRowSec, 4).Value <> ""
            Debug.Print "Loop B " & Sheets("LOOKUP_VALUES").Cells(2, 2).Value
            intRowSec = intRowSec + 1
        Loop
        intRowDiv = intRowDiv + 1
    Loop

End Sub

你应该在立即窗口上获得一些信息(按 Ctrl + G 查看它)并尝试确保它完全按照你想要的方式是