|
▼いとう さん:
データは計上日で降順ソートされている前提
Option Explicit
Sub test()
Dim dic As Object, aryl As Object
Dim 品目CD As String, 単価 As Long, 金額 As Long
Dim v, i As Long, k As String
v = Cells(1).CurrentRegion.Value
Set dic = CreateObject("scripting.dictionary")
Set aryl = CreateObject("system.collections.arraylist")
For i = UBound(v) To 2 Step -1
品目CD = v(i, 1)
単価 = v(i, 3)
金額 = v(i, 4)
If 金額 > 0 Then
k = 品目CD & vbTab & 単価 & vbTab & 金額
aryl.Add i
If Not dic.exists(k) Then
Set dic(k) = CreateObject("system.collections.stack")
End If
dic(k).push i
Else
k = 品目CD & vbTab & 単価 & vbTab & 金額 * -1
aryl.Remove dic(k).pop
End If
Next
aryl.Add 1
aryl.Reverse
v = Application.Index(v, Application.Transpose(aryl.toarray), Array(1, 2, 3, 4))
Worksheets.Add.Cells(1).Resize(UBound(v), 4).Value = v
End Sub
|
|