You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Excel宏自定义打印:在打印页间插入指定工作表页面

实现自定义打印顺序的Excel宏方案

当然可行!这种按指定顺序混合打印不同工作表页面的需求,完全可以通过Excel VBA宏实现,而且能让你按想要的顺序进入打印预览。下面分两种场景给出具体解决方案:

一、直接按顺序打印(适合无需预览直接输出)

如果你只需要按顺序完成打印任务,不需要一次性预览所有页面,可以用这段VBA代码:

Sub CustomPrintOrder()
    ' 打印Sheet1的第1-2页
    Sheets("Sheet1").PrintOut From:=1, To:=2, Preview:=False
    
    ' 插入打印Sheet2的第1页
    Sheets("Sheet2").PrintOut From:=1, To:=1, Preview:=False
    
    ' 打印Sheet1剩余的第3-4页
    Sheets("Sheet1").PrintOut From:=3, To:=4, Preview:=False
End Sub

这段代码会依次执行三个打印任务,严格按照你要求的顺序输出纸张。

二、一次性预览所有页面(满足你的理想需求)

如果希望在一个打印预览窗口里看到所有页面按顺序排列,我们需要先把目标页面的内容合并到一个临时工作表,再对这个临时表进行预览:

Sub CustomPrintPreview()
    Dim tempSheet As Worksheet
    Dim lastRow As Long
    Dim currentRow As Long
    Dim startRow As Long
    
    ' 创建临时工作表
    Set tempSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    tempSheet.Name = "TempPrintSheet"
    currentRow = 1
    
    ' 复制Sheet1的第1-2页内容到临时表
    Sheets("Sheet1").Activate
    ' 通过水平分页符定位第2页末尾行号
    If Sheets("Sheet1").HPageBreaks.Count >= 2 Then
        lastRow = Sheets("Sheet1").HPageBreaks(2).Location.Row - 1
    Else
        lastRow = Sheets("Sheet1").UsedRange.Rows(Sheets("Sheet1").UsedRange.Rows.Count).Row
    End If
    Sheets("Sheet1").Rows("1:" & lastRow).Copy Destination:=tempSheet.Rows(currentRow)
    currentRow = tempSheet.Cells(tempSheet.Rows.Count, "A").End(xlUp).Row + 2 ' 留空行模拟页面分隔
    
    ' 复制Sheet2的第1页内容到临时表
    Sheets("Sheet2").Activate
    If Sheets("Sheet2").HPageBreaks.Count >= 1 Then
        lastRow = Sheets("Sheet2").HPageBreaks(1).Location.Row - 1
    Else
        lastRow = Sheets("Sheet2").UsedRange.Rows(Sheets("Sheet2").UsedRange.Rows.Count).Row
    End If
    Sheets("Sheet2").Rows("1:" & lastRow).Copy Destination:=tempSheet.Rows(currentRow)
    currentRow = tempSheet.Cells(tempSheet.Rows.Count, "A").End(xlUp).Row + 2
    
    ' 复制Sheet1的第3-4页内容到临时表
    Sheets("Sheet1").Activate
    If Sheets("Sheet1").HPageBreaks.Count >= 2 Then
        startRow = Sheets("Sheet1").HPageBreaks(2).Location.Row
        ' 定位第4页末尾行号
        If Sheets("Sheet1").HPageBreaks.Count >= 4 Then
            lastRow = Sheets("Sheet1").HPageBreaks(4).Location.Row - 1
        Else
            lastRow = Sheets("Sheet1").UsedRange.Rows(Sheets("Sheet1").UsedRange.Rows.Count).Row
        End If
        Sheets("Sheet1").Rows(startRow & ":" & lastRow).Copy Destination:=tempSheet.Rows(currentRow)
    End If
    
    ' 设置临时表的手动分页符,还原原页面分隔逻辑
    tempSheet.HPageBreaks.Add Before:=tempSheet.Rows(currentRow - 1)
    If Sheets("Sheet1").HPageBreaks.Count >= 3 Then
        tempSheet.HPageBreaks.Add Before:=tempSheet.Rows(currentRow + Sheets("Sheet1").HPageBreaks(3).Location.Row - startRow)
    End If
    
    ' 进入打印预览
    tempSheet.PrintPreview
    
    ' 预览结束后自动删除临时工作表(可注释此行保留临时表)
    Application.DisplayAlerts = False
    tempSheet.Delete
    Application.DisplayAlerts = True
End Sub

注意事项:

  • 代码通过HPageBreaks(水平分页符)定位各页面的行范围,无论是自动分页还是手动设置的分页符都能适配。
  • 临时工作表会在预览后自动删除,如果你需要检查合并后的内容,可以注释掉删除临时表的代码段。

内容的提问来源于stack exchange,提问作者Ben Edwards

火山引擎 最新活动