师父,能否告知一下excel如何自动依次打印
发布网友
发布时间:2022-04-22 05:34
我来回答
共1个回答
热心网友
时间:2023-06-28 05:49
这个是可以用VBA实现的,先按照批次把表分成N个分表,再选中所有分表一次性打印出来。
你试下这个代码:
Sub XinJian() '按列新建工作表
On Error Resume Next
Set Rng = Application.InputBox("选择按哪一列进行新建工作表。" & vbNewLine & vbNewLine & "请确保第一行为标题!", Default:=ActiveCell.Address, Title:=123, Type:=8)
If Err <> 0 Then
MsgBox "请选择一个单元格对象。", vbInformation, 123
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
L = Rng.Column
Set d = CreateObject("Scripting.Dictionary")
Set Rng = Range(Cells(2, L), Cells(65535, L).End(xlUp))
Set sht = Rng.Parent
Set Temp = sht.Range(Cells(1, 1), sht.Cells.SpecialCells(xlCellTypeLastCell))
For Each n In Temp
If n.MergeCells = True Then
MsgBox "请取消所有合并单元格!", vbInformation, 123
Exit Sub
End If
Next n
For Each n In Rng
d(n.Value) = ""
Next n
For i = 0 To d.Count - 1 Step 1
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = CStr(d.keys()(i))
sht.Rows(1).Copy ActiveSheet.[a1] '复制表头
sht.Rows(1).Copy
[a1].PasteSpecial Paste:=xlPasteColumnWidths '列宽
Next i
sht.Activate
For Each n In Rng
n.EntireRow.Copy Worksheets(CStr(n.Value)).Cells(65535, L).End(xlUp).Offset(1, 1 - L)
Next n
sht.[a1].Select
Set Rng = Nothing
Set d = Nothing
Set sht = Nothing
Set Temp = Nothing
End Sub
追问这个VBA确实有用,但是能生成到一个表格里面,设置好打印格式就好了。
追答你的打印格式应该都是一样的,你录制一个设置打印格式的宏,然后放到这里就行:
'你录制的宏
[a1].PasteSpecial Paste:=xlPasteColumnWidths '列宽
Next i