Board logo

標題: [發問] Excel合併列印 [打印本頁]

作者: max67424    時間: 2023-6-14 03:50     標題: Excel合併列印

本帖最後由 max67424 於 2023-6-14 03:52 編輯

[attach]36578[/attach]
[attach]36579[/attach]

請益論壇裡各位VBA高手,該如何撰寫VBA,才能將工作表的內容做到像word的合併列印功能,謝謝。
[attach]36582[/attach]
作者: Andy2483    時間: 2023-6-14 11:09

回復 1# max67424


    謝謝論壇,謝謝前輩發表此主題與範例
後學藉此帖練習VBA陣列,學習方案如下,請前輩參考

資料表:
[attach]36583[/attach]

執行結果:
[attach]36584[/attach]


Option Explicit
Sub TEST()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim Brr, A, B, Z, i&, j%, xR As Range
A = Array(1, 2, 5, 6, 11, 15, 16, 17, 19, 22, 27, 28, 31, 35, 40)
B = Array(2, 3, 4, 17, 5, 6, 7, 8, 9, 10, 11, 16, 12, 13, 14)
Brr = Range([收料標籤!Q2], [收料標籤!A65536].End(3))
Set xR = [標籤版型!A1:E8]
On Error Resume Next: Sheets("合併列印").Delete: On Error GoTo 0
Sheets("標籤版型").Copy after:=Worksheets(Sheets.Count)
With Sheets(Sheets.Count)
   .Name = "合併列印": .[A:E].Clear
   With .DrawingObjects
      If .Count > 0 Then .Delete
   End With
End With
For i = 1 To UBound(Brr)
   If Brr(i, 5) = "" Then Exit For
   For j = 0 To UBound(A): xR(A(j)) = Brr(i, B(j)): Next
   xR(1) = xR(1) & "-": xR(16) = "倉:" & xR(16) & "/"
   xR(17) = "儲:" & xR(17) & "/": xR(28) = "(" & xR(28) & ")"
   xR(40) = xR(40) & Brr(i, UBound(A) + 1)
   xR.Copy Sheets("合併列印").Cells((i - 1) * 8 + 1, 1)
Next
Set xR = Nothing: Erase Brr, A, B
End Sub
作者: max67424    時間: 2023-6-15 02:52

回復 2# Andy2483
感謝Andy 大大指點迷津:lol
作者: Andy2483    時間: 2023-6-15 08:31

本帖最後由 Andy2483 於 2023-6-15 08:36 編輯

回復 3# max67424


    謝謝論壇,謝謝前輩回復
後學複習了一下,複習心得註解如下,請前輩參考,請各位前輩指教

Option Explicit
Public SELrr, SEL%
Sub TEST()
Application.DisplayAlerts = False
'↑令程序執行不要問刪不刪(刪)
Application.ScreenUpdating = False
'↑令螢幕不隨程序執行結果做變化
Dim Brr, A, B, Z, i&, j%, xR As Range
'↑宣告變數
A = Array(1, 2, 5, 6, 11, 15, 16, 17, 19, 22, 27, 28, 31, 35, 40)
'↑令A變數是 一維陣列(索引號0~14,這是標籤版型的格號)
B = Array(2, 3, 4, 17, 5, 6, 7, 8, 9, 10, 11, 16, 12, 13, 14)
'↑令B變數是 一維陣列(索引號0~14,這是收料標籤的欄號)
If SEL = 1 Then Brr = SELrr Else: Brr = Range([收料標籤!Q2], [收料標籤!A65536].End(3))
'↑如果SEL變數是 1?,是就令Brr變數是SELrr變數(二維陣列),
'否則就令Brr變數是 二維陣列,以收料標籤表的A~Q欄儲存格值帶入陣列中

Set xR = [標籤版型!A1:E8]
'↑令xR變數是 [標籤版型!A1:E8](物件:儲存格)
On Error Resume Next: Sheets("合併列印").Delete: On Error GoTo 0
'↑令"合併列印"表 刪除
Sheets("標籤版型").Copy after:=Worksheets(Sheets.Count)
'↑令"標籤版型"表複製一份到 工作表索引最後
With Sheets(Sheets.Count)
   .Name = "合併列印": .[A:E].Clear
   '↑令複製的工作表改名為"合併列印",並清除A~E欄
   With .DrawingObjects
      If .Count > 0 Then .Delete
   End With
   '↑令刪除圖片.圖案....等
End With
For i = 1 To UBound(Brr)
   If Brr(i, 5) = "" Then Exit For
   For j = 0 To UBound(A): xR(A(j)) = Brr(i, B(j)): Next
   xR(1) = xR(1) & "-": xR(16) = "倉:" & xR(16) & "/"
   xR(17) = "儲:" & xR(17) & "/": xR(28) = "(" & xR(28) & ")"
   xR(40) = xR(40) & Brr(i, 15)
   xR.Copy Sheets("合併列印").Cells((i - 1) * 8 + 1, 1)
Next
'↑設順迴圈將 "收料標籤"表資料帶入 [標籤版型!A1:E8]後,
'複製到 "合併列印"表

SEL = 0
'↑令SEL變數為 0
Set xR = Nothing: Erase Brr, A, B
'↑令釋放變數
End Sub

Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
With Target
   Cancel = True
   If Rows.Count = .EntireRow.Rows.Count Then Exit Sub
   If .Row = 1 Then Exit Sub
   Set SELrr = Intersect(.EntireRow, [A:Q])
   If InStr(SELrr.Address, ",") Then Exit Sub
   SEL = 1:  Call TEST
End With
End Sub
作者: max67424    時間: 2023-6-16 03:35

回復 4# Andy2483
感謝Andy 大提供註解,學習起來更輕鬆了。感恩




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)