Option Explicit
Sub test1()
Dim ar, br, cr(), dr, i&, j&, r&, n&, Rng As Range, shp As Shape, strBar$
Application.ScreenUpdating = False
br = [{"A1",2;"C1",3;"B6",5;"B7",6;"B4",8;"B5",10;"B3",11;"B2",14;"D7",16;"B8",17}]
Set Rng = Worksheets("模版").[A1:E9]
n = Sheets("数据源").Cells(Rows.Count, "A").End(xlUp).Row
With Sheets("数据源").Range("A1:R" & n)
ar = .Value
For i = 3 To UBound(ar)
If ar(i, UBound(ar, 2) - 1) <> 0 And .Rows(i).EntireRow.Hidden = False Then
r = r + 1
ReDim Preserve cr(1 To r)
cr(r) = i
ar(i, UBound(ar, 2)) = "已打印"
End If
Next i
.Value = ar
End With
If Join(cr) <> "" Then
dr = arrLocated(UBound(cr), 2, 9, 5)
With Worksheets("最终生成标签的样式")
.Cells.Delete
For Each shp In .Shapes
If shp.Type <> msoFormControl Then shp.Delete
Next
For i = 1 To UBound(cr)
rngCopyToSame Rng, .Cells(dr(i, 1), dr(i, 2))
With .Cells(dr(i, 1), dr(i, 2))
For j = 1 To UBound(br)
.Range(br(j, 1)) = ar(cr(i), br(j, 2))
Next j
.Range("C8") = ar(1, 11)
End With
With .Cells(dr(i, 1), dr(i, 2) + 3)
strBar = .Offset(, -1).Value
.Select
With .Parent.OLEObjects.Add(classtype:="BARCODE.BarCodeCtrl.1", Height:=.Height, Width:=.Width, Left:=.Left, Top:=.Top)
.Object.Style = 7
.Object.Value = strBar
End With
End With
Next
End With
Else
MsgBox "不满足需要生成的数据!"
End If
Application.ScreenUpdating = True
Beep
End Sub
Function rngCopyToSame(ByVal rngSel As Range, ByVal rngTarget As Range)
Dim i&
rngSel.Copy
rngTarget.PasteSpecial xlPasteColumnWidths
rngSel.Copy rngTarget
With rngTarget.Resize(rngSel.Rows.Count, rngSel.Columns.Count)
For i = 1 To .Rows.Count
.Rows(i).RowHeight = rngSel.Rows(i).RowHeight
Next i
End With
End Function
Function arrLocated(ByVal iCount&, ByVal iColNum&, ByVal iStepRows&, ByVal iStepColumns&)
Dim ar, i&, n&, y&, x&
ReDim ar(1 To iCount, 1 To 2)
For i = 1 To UBound(ar)
n = IIf(i Mod iColNum = 0, iColNum, i Mod iColNum)
y = (-Int(-(i / iColNum)) - 1) * iStepRows + 1
x = (n - 1) * iStepColumns + 1
ar(i, 1) = y: ar(i, 2) = x
Next i
arrLocated = ar
End Function
|