|
试试这样符不符合?
- Sub PrintRowsInBatchesWithPreview()
- Dim ws As Worksheet
- Dim selectedColumns As String
- Dim columnArray() As String
- Dim i As Integer
- Dim printRange As Range
- Dim rowsPerPage As Integer
- Dim lastRow As Long
- Dim currentRow As Long
- Dim batchStartRow As Long
- Dim batchEndRow As Long
- Dim userResponse As VbMsgBoxResult
- ' 输入要打印的列号或列字母,用逗号分隔
- selectedColumns = "A,B" ' 例如打印A列、C列和E列
- ' 每页打印的行数
- rowsPerPage = 5 ' 例如每页打印5行
- ' 将列号或字母转换为数组
- columnArray = Split(selectedColumns, ",")
- ' 选择要打印的工作表
- Set ws = ThisWorkbook.Sheets("Sheet1") ' 这里将"Sheet1"替换为你的工作表名
- ' 获取工作表的最后一行
- lastRow = ws.Cells(ws.Rows.Count, columnArray(0)).End(xlUp).Row
- currentRow = 1
- ' 遍历并打印每一批次的行
- Do While currentRow <= lastRow
- batchStartRow = currentRow
- batchEndRow = Application.Min(currentRow + rowsPerPage - 1, lastRow)
- ' 初始化打印范围
- Set printRange = ws.Range(columnArray(0) & batchStartRow & ":" & columnArray(0) & batchEndRow)
- ' 将所有指定列的范围合并
- For i = 1 To UBound(columnArray)
- Set printRange = Union(printRange, ws.Range(columnArray(i) & batchStartRow & ":" & columnArray(i) & batchEndRow))
- Next i
- ' 设置打印范围
- ws.PageSetup.PrintArea = printRange.Address
- ' 先预览打印内容
- ws.PrintPreview
- ' 提示用户是否要打印
- userResponse = MsgBox("要打印当前页吗?", vbYesNo + vbQuestion, "打印确认")
- If userResponse = vbYes Then
- ws.PrintOut
- End If
- ' 移动到下一批次的起始行
- currentRow = batchEndRow + 1
- Loop
- MsgBox "打印操作完成!"
- End Sub
复制代码
|
|