|
楼主 |
发表于 2024-7-13 09:45
|
显示全部楼层
Sub 自动打印()
Dim wsdata As Worksheet, wstemp As Worksheet
Dim lastrow As Long, i As Long, j As Long, k As Long
Dim printRange As Range
Set wsdata = ThisWorkbook.Worksheets("sheet3") ' 数据源
Set wstemp = ThisWorkbook.Worksheets("sheet1") ' 打印表格
lastrow = wsdata.Range("G44444").End(xlUp).Row
For i = 2 To lastrow
If Not IsEmpty(wsdata.Range("S" & i).Value) Then
' 设置打印区域的值
wstemp.Range("E4").Value = wsdata.Range("L" & i).Value
wstemp.Range("A6").Value = wsdata.Range("A" & i).Value
wstemp.Range("E6").Value = wsdata.Range("E" & i).Value
' 动态填充表格
k = 6 + 1
For j = i + 1 To i + 7
If wsdata.Range("B" & j).Value = wsdata.Range("B" & j - 1).Value Then
wstemp.Range("A" & k).Value = wsdata.Range("A" & j).Value
wstemp.Range("E" & k).Value = wsdata.Range("E" & j).Value
k = k + 1
i = i + 1 ' 跳过已处理的行
Else
Exit For
End If
Next j
' 打印当前打印表格
wstemp.PrintOut
' 清除打印表格中的内容(保留标题行)
Set printRange = wstemp.Range("A6:E12")
printRange.ClearContents
End If
Next i
MsgBox "所有信息已经打印完毕"
End Sub
从这个扩展得出,求助 |
|