|
Sub 插入行数据()
Dim ar As Variant, br As Variant
Dim i As Long, r As Long, rs As Long
Dim cr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Sheets("销售数据")
r = .Cells(Rows.Count, 2).End(xlUp).Row
If r < 2 Then MsgBox "销售数据工作表为空!": End
ar = .Range("a2:k" & r)
End With
For i = 2 To UBound(ar)
If ar(i, 5) <> "" Then
If Not d.exists(ar(i, 5)) Then Set d(ar(i, 5)) = CreateObject("scripting.dictionary")
d(ar(i, 5))(i) = ""
End If
Next i
With Sheets("原始")
rs = .Cells(Rows.Count, 2).End(xlUp).Row
If rs < 2 Then MsgBox "原始工作表为空!": End
br = .Range("a1:k" & rs)
ReDim cr(1 To UBound(ar) + UBound(br), 1 To UBound(br, 2))
For i = 2 To UBound(br)
If br(i, 5) <> "" Then
n = n + 1
For j = 1 To UBound(br, 2)
cr(n, j) = br(i, j)
Next j
If d.exists(br(i, 5)) Then
For Each k In d(br(i, 5))
n = n + 1
For j = 1 To UBound(ar, 2)
cr(n, j) = ar(k, j)
Next j
Next k
End If
End If
Next i
.Range("a2:k" & rs) = Empty
.[a2].Resize(n, UBound(cr, 2)) = cr
.[a2].Resize(n, UBound(cr, 2)).Borders.LineStyle = 1
End With
MsgBox "ok!"
End Sub |
|