Sub GetLB2()
Dim a, e, i&, ii&, w, s$(1), t&, NewItem
If (Me.ComboBox1 = "") + (Me.ComboBox1 = "all") Then
With Sheets("sheet1")
s(0) = .Range("b2", .Range("b" & Rows.Count).End(xlUp)).Address(, , , 1)
End With
With Sheets("balance first of duration")
s(1) = .Range("b2", .Range("b" & Rows.Count).End(xlUp)).Address
NewItem = Filter(.Evaluate("transpose(if(isna(match(" & s(1) & "," & s(0) & ",0))," & s(1) & "))"), False, 0)
End With
End If
a = Me.ListBox1.List
With CreateObject("Scripting.Dictionary")
.Add "item", Array("ITEM", "Name", "Debit", "Credit", "Balance")
If IsArray(NewItem) Then
For Each e In NewItem
.Item(e) = Array("", e, 0, 0, 0)
Next
End If
For i = 1 To UBound(a, 1)
If Not .exists(a(i, 1)) Then
.Item(a(i, 1)) = Array("", a(i, 1), a(i, 4), a(i, 5), a(i, 4) - a(i, 5))
Else
w = .Item(a(i, 1))
For ii = 2 To 3
w(ii) = w(ii) + a(i, ii + 2)
Next
w(4) = w(2) - w(3)
.Item(a(i, 1)) = w
End If
Next
a = Sheets("balance first of duration").[a1].CurrentRegion
For i = 2 To UBound(a, 1)
If .exists(a(i, 2)) Then
w = .Item(a(i, 2))
t = IIf(a(i, 4) > 0, 2, 3)
w(t) = w(t) + Abs(a(i, 4))
w(4) = w(2) - w(3)
.Item(a(i, 2)) = w
End If
Next
.Add "Total", Array("TOTAL", "", "", "", 0)
a = Application.Index(.items, 0, 0)
End With
mySort a
Me.ListBox2.List = a End Sub