I'm trying to use dictionary for 3 different columns with COUNTIF and SUMIF getting the value from another sheet. I'm having a hard time getting the right code to get the value from another sheet, below is my currently using code. Any help will do thanks.
Transact Sheet
Invoice Sheet
Transact Sheet
Invoice Sheet
VBA Code:
Sub CTotals()
Dim arr, ws, ws2, rng As Range, keyCols, valueCol As Long, destCol As Long, i As Long, frm As String, sep As String
Dim t, dict, arrOut(), arrValues(), v, tmp, n As Long
keyCols = 2
valueCol = 6
destCol = 4
t = Timer
Set ws = ThisWorkbook.Worksheets("Transact")
Set ws2 = ThisWorkbook.Worksheets("Invoice")
Set rng = ws.Range("A1").CurrentRegion
Set rng2 = ws2.Range("A6").CurrentRegion
n = rng.Rows.Count - 1
n2 = rng2.Rows.Count - 1
Set rng = rng.Offset(1, 0).Resize(n)
Set rng2 = rng2.Offset(1, 0).Resize(n)
For i = 2 To (keyCols)
frm = frm & rng2.Columns(keyCols).Address
Next i
arr = ws.Evaluate(frm)
arrValues = rng.Columns(valueCol).Value
ReDim arrOut(1 To n, 1 To 1)
Set dict = CreateObject("scripting.dictionary")
For i = 1 To n
v = arr(i, 1)
If Not dict.exists(v) Then dict(v) = Array(0, 0)
tmp = dict(v)
tmp(0) = tmp(0) + 1
tmp(1) = tmp(1) + arrValues(i, 1)
dict(v) = tmp
Next i
For i = 1 To n
arrOut(i, 1) = dict(arr(i, 1))(1)
Next i
rng2.Columns(destCol).Value = arrOut
Debug.Print "Checked " & n & " rows in " & Timer - t & " secs"
End Sub