重复标题,但在Excel的每个页面中重新计算公式

重复标题,但在Excel的每个页面中重新计算公式,excel,excel-formula,Excel,Excel Formula,我已经创建了一个40页的工作表。我知道打印时不可能重复底部单元格,所以我只是重复了顶行(其中包含一个求和公式),以使纸张更紧凑。但问题是,第一行的求和公式只计算第一页的数据。打印时可以计算每页中的数据,就像我们在Microsoft Access的标题中可以做的那样。这应该可以做到: Sub SubSpecialPrinting() 'Declarations. Dim IntCounter01 As Integer Dim IntPagesToBePrinted

我已经创建了一个40页的工作表。我知道打印时不可能重复底部单元格,所以我只是重复了
顶行
(其中包含一个
求和
公式),以使纸张更紧凑。但问题是,第一行的求和公式只计算第一页的数据。打印时可以计算每页中的数据,就像我们在Microsoft Access的标题中可以做的那样。

这应该可以做到:

Sub SubSpecialPrinting()
    
    'Declarations.
    Dim IntCounter01 As Integer
    Dim IntPagesToBePrinted As Integer
    Dim LngRow As Long
    Dim RngRangeWithFormula As Range
    Dim RngSumRange As Range
    Dim StrExistingFormula As String
    Dim WksWorksheet01 As Worksheet
    
    'Setting variables.
    Set RngRangeWithFormula = Range("I1")
    Set RngSumRange = Range("A1:A10")
    Set WksWorksheet01 = RngSumRange.Parent
    StrExistingFormula = RngRangeWithFormula.Formula
    IntPagesToBePrinted = ExecuteExcel4Macro("GET.DOCUMENT(50)")
    
    'Disabling interruption in case of error.
    On Error Resume Next
    
    'Covering all the pages to be printed.
    For IntCounter01 = 1 To IntPagesToBePrinted
        
        'Setting the formula in RngRangeWithFormula.
        RngRangeWithFormula.Value = Excel.WorksheetFunction.Sum(RngSumRange)
        
        'Printing the single given page.
        WksWorksheet01.PrintOut From:=IntCounter01, _
                                To:=IntCounter01, _
                                Copies:=1, _
                                Collate:=True, _
                                IgnorePrintAreas:=False
        
        'Setting LngRow equal rows of the page just printed.
        LngRow = WksWorksheet01.HPageBreaks(IntCounter01).Location.Row - 1 - LngRow
        
        'Setting RngSumRange for the next page to be printed.
        Set RngSumRange = RngSumRange.Offset(LngRow, 0)
        
    Next
    
    'Reactivating interruption in case of error.
    On Error GoTo 0
    
    'Reporting the pre-existing formula in RngRangeWithFormula.
    RngRangeWithFormula.Formula = StrExistingFormula
    
End Sub
根据需要更改RngRangeWithFormula和RngSumRange值。子例程将逐个打印打印区域的页面。打印前,会将RngRangeWithFormula的值与RngSumRange中的值之和进行更改;后者将针对每个页面进行更改,向下移动给定页面包含的行数。在子例程末尾,RngRangeWithFormula将具有与运行子例程之前相同的公式

由于要正确打印表单,需要调用子例程,因此我花了几个小时寻找通过函数获得相同结果的方法。我已经找到了如何知道给定单元格所属的页面,但还没有找到在打印时动态使用页码的方法。最后,我接受了子程序作为解决方法。不过,为了避免无意义地打印“不正确”的工作表,并让用户更容易使用,我编写了以下额外代码,放在工作簿模块中:

Public PubBlnDedicatedPrinting As Boolean
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    
    'Checking if a dedicated printing is in process.
    If PubBlnDedicatedPrinting = False Then
        
        'Checking a specific sheet is active.
        If ActiveSheet.Name = "INSERT YOUR SHEET NAME HERE" Then
            
            'Asking if the user want to use the dedicated macro for printing.
            Select Case MsgBox("Do you want to run the dedicated macro for printing?", vbYesNoCancel, "Dedicated macro")
                Case Is = 6
                    'Setting PubBlnDedicatedPrinting value is set to true.
                    PubBlnDedicatedPrinting = True
                    'Running the dedicated macro.
                    Call SubSpecialPrinting
                    'Setting PubBlnDedicatedPrinting value is set to false.
                    PubBlnDedicatedPrinting = False
                    'Canceling any other printing procedure.
                    Cancel = True
                Case Is = 7
                    'Proceeding with a classical printing.
                    Cancel = False
                Case Is = 2
                    'Canceling any printing procedure.
                    Cancel = True
            End Select
        End If
    End If
    
End Sub

为使其有效,您需要将“在此处插入您的工作表名称”与工作表名称合并。一旦用户尝试打印给定的工作表,Excel将询问他是要运行专用宏还是要以经典方式打印,或两者都不打印,或是以其他方式打印。

可以在多个工作表中求和。您可以使用感叹号,后跟工作表名称,后跟范围地址,从给定工作表锁定给定范围。像
Sheet1!A1
。这个问题还包含一些关于感叹号用法的更有趣的细节。打印时是否要更改公式?VBA解决方案适合您吗?@EvilBlueMonkey我不需要锁定范围,我只需要动态更改范围。e、 g.页眉在第1页使用=Sum(A1-A10),在第2页使用=Sum(A11-A20)。我可以在MS Access上轻松完成此操作,但不能在excel上完成。VBA对我来说很好。我希望你能给我指出一些有用的链接。谢谢,好像不起作用。我已经更改了RngGrangeWithFormula和RngSumRange,更改了“在此处插入您的工作表名称”,并将我的工作簿保存为“已启用宏”*.xlsm。1)但没有错误?2) 您为每个变量分配了哪些值?3) 您是否手动运行子例程子专业打印?4) 如果出现错误,在打开中断后运行它是否会出现任何错误(需要停用一行代码才能实现这一点)?5) 您要打印的工作表是否已激活?6) 是否未打印任何内容或打印时使用了RngRangeWithFormula中的错误值?1)无错误。2) RngRangeWith公式=范围(“A15”),RngSumRange=范围(“A20:A30”)。3) 正常打印时不工作(Alt+F11),但当我运行模块时,它工作,但(A)不考虑分页符,(B)将每页打印为“单独的纸张/文档”,而不是打印“单页多页”;(C)取消打印不工作,我必须等到它将所有页面作为单独的纸张完成后台打印(37页,确实需要很长时间)4)我似乎不明白这一点。(5) 是的,工作表已激活。打印预览中未显示总和,但它是在我运行模块(VBA)时打印的。(6) 所有页面都打印出来了,第一页得到了正确的总数,但随后的页面没有。谢谢你的帮助。好的。2) 它们看起来很好。3) 好的,我们稍后可能会检查为什么正常打印不起作用;(A) 我想我需要提取你的数据,即使是假值;我只需要一个你拥有的和结果应该是什么的模型;A列的前3页可能是enouth;(B&C)ye,我一直在寻找一种方法使它成为一个单一的打印文档,但没有发现任何有用的东西;无论如何,您可能会感兴趣,因为它提供了一种以旧方式获取打印预览的方法(因此任何与打印相关的宏都将运行以创建[…]