Sub test()
Dim a, i As Long, t As Long, dic As Object
Const keyCol As Long = 9, sumCol As Long = 5
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("statement").[a4].CurrentRegion.Value
For i = 2 To UBound(a, 1)
If Not dic.exists(a(i, keyCol)) Then
dic(a(i, keyCol)) = Array(CreateObject("Scripting.Dictionary"), _
CreateObject("Scripting.Dictionary"))
End If
t = IIf(a(i, sumCol) >= 0, 0, 1)
dic(a(i, keyCol))(t)(a(i, sumCol)) = Trim$(dic(a(i, keyCol))(t)(a(i, sumCol)) & " " & i)
Next
Reconciled a, dic
Unreconciled a, dic
End Sub
Sub Reconciled(a, dic As Object)
Dim e, s, w, x, y, temp
Dim i As Long, ii As Long, n As Long
ReDim b(1 To 1)
For Each e In dic
For Each s In dic(e)(0)
If dic(e)(1).exists(s * -1) Then
x = Split(dic(e)(0)(s))
y = Split(dic(e)(1)(s * -1))
For i = 0 To Application.Min(UBound(x), UBound(y))
ReDim temp(1 To UBound(a, 2) * 2 + 1)
For ii = 1 To UBound(a, 2)
temp(ii) = a(y(i), ii)
temp(ii + UBound(a, 2) + 1) = a(x(i), ii)
Next
b(UBound(b)) = temp
ReDim Preserve b(1 To UBound(b) + 1)
x(i) = "": y(i) = ""
Next
x = Trim$(Join(x))
If x = "" Then
dic(e)(0).Remove s
Else
dic(e)(0)(s) = x
End If
y = Trim$(Join(y))
If y = "" Then
dic(e)(1).Remove s * -1
Else
dic(e)(1)(s * -1) = y
End If
End If
Next
Next
ReDim Preserve b(1 To UBound(b) - 1)
With Sheets("reconciled")
.Rows("5:" & Application.Max(5, .Cells.SpecialCells(11).Row)).ClearContents
If b(1)(1) <> "" Then
ReDim Preserve b(1 To UBound(b) - 1)
.[a5].Resize(UBound(b), UBound(a, 2) * 2 + 1) = Application.Index(b, 0, 0)
End If
End With
End Sub
Sub Unreconciled(a, dic)
Dim e, s, v, x, n As Long, i As Long, ii As Long, temp
ReDim b(1 To 1), c(1 To 1), temp(1 To UBound(a, 2))
For Each e In dic
n = 0
For i = 0 To 1
For Each s In dic(e)(i)
For Each v In Split(dic(e)(i)(s))
For ii = 1 To UBound(a, 2)
temp(ii) = a(v, ii)
Next
Next
If i = 0 Then
b(UBound(b)) = temp
ReDim Preserve b(1 To UBound(b) + 1)
Else
c(UBound(c)) = temp
ReDim Preserve c(1 To UBound(c) + 1)
End If
Next
Next
Next
With Sheets("unreconciled")
.Rows("5:" & Application.Max(5, .Cells.SpecialCells(11).Row)).ClearContents
If b(1)(1) <> "" Then
ReDim Preserve b(1 To UBound(b) - 1)
.[a5].Resize(UBound(b), UBound(a, 2)) = Application.Index(b, 0, 0)
End If
If c(1)(1) <> "" Then
ReDim Preserve c(1 To UBound(c) - 1)
.[n5].Resize(UBound(c), UBound(a, 2)) = Application.Index(c, 0, 0)
End If
End With
End Sub
Bookmarks