【问题标题】:excel report generate with dynamic pages使用动态页面生成 excel 报告
【发布时间】:2017-12-05 00:24:58
【问题描述】:

我正在尝试为公司的每个部门打印一份 Excel 报告,以获取一份包含其下方部分的附录报告的报告。 IE。人力资源部下设招聘、人员配备、合规、包容和多元化、领导力发展等部门。

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

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

【问题讨论】:

  • 您的工作簿样本可能会有所帮助,您可能已经知道 Sheets(2018 CoverPage").Activate 缺少初始“,您也正在激活 Sheets 并选择它们,但这可以在没有任何激活或选择,我不理解的是 Sheet Lookup_values 的布局......

标签: vba excel


【解决方案1】:

如果只打印一个部分,这意味着你的逻辑在某处被破坏了。你有两个循环——一个内循环和一个外循环。确保每个都正确循环,就像这个一样简单:

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 来查看)并尝试确保它完全符合您的要求是。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2020-12-15
    相关资源
    最近更新 更多