Hello guys so I obtained this VBA code which helps me highlight Matching Values on Columns C & D. And I would like to expand it a bit.
The macro helps me go from this:
To this:
Ideally, I would like for the macro to be able to recognize that rows 5 and 6 occurred on the same "Date" and also have the same "Code" and the sum of the values on Column C(33.15 & 10.12) add up to 43.27 which is a value on Column D. So I would like for the final result to look something like this:
Bonus question: Is there a way of getting this Macro to not use White as one of the interior fill colors? Cells C9 & D2 have matching values and are filled with white. I would like to avoid that if possible.
Thank you.
VBA Code:
Sub MatchiNV()
Dim c As Range, r As Range, f As Range, n As Long, cell As String
Columns("C:D").NumberFormat = "General"
n = 2
Set r = Range("C2", Range("C" & Rows.Count).End(3))
r.Interior.ColorIndex = xlNone
For Each c In Range("D2", Range("D" & Rows.Count).End(3))
If c.Value > 0 Then
Set f = r.Find(c, , xlValues, xlWhole)
If Not f Is Nothing Then
cell = f.Address
Do
If f.Interior.ColorIndex = xlNone Then
f.Interior.ColorIndex = n
c.Interior.ColorIndex = n
n = n + 1
If n = 56 Then n = 7
Exit Do
End If
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If
End If
Next
End Sub
The macro helps me go from this:
Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Date | Code | Total | Outstanding | ||
2 | 1/1/2023 | T01 | 50.56 | 500.25 | ||
3 | 1/1/2023 | T02 | 30.32 | 58.43 | ||
4 | 1/1/2023 | T03 | 58.43 | 30.32 | ||
5 | 1/2/2023 | T04 | 33.15 | 289.65 | ||
6 | 1/2/2023 | T04 | 10.12 | 43.27 | ||
7 | 1/2/2023 | T05 | 15.63 | 300 | ||
8 | 1/2/2023 | T01 | 289.65 | 50.56 | ||
9 | 1/3/2023 | T06 | 500.25 | 600.5 | ||
10 | 1/3/2023 | T04 | 647.5 | 15.63 | ||
Source |
To this:
Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Date | Code | Total | Outstanding | ||
2 | 1/1/2023 | T01 | 50.56 | 500.25 | ||
3 | 1/1/2023 | T02 | 30.32 | 58.43 | ||
4 | 1/1/2023 | T03 | 58.43 | 30.32 | ||
5 | 1/2/2023 | T04 | 33.15 | 289.65 | ||
6 | 1/2/2023 | T04 | 10.12 | 43.27 | ||
7 | 1/2/2023 | T05 | 15.63 | 300 | ||
8 | 1/2/2023 | T01 | 289.65 | 50.56 | ||
9 | 1/3/2023 | T06 | 500.25 | 600.5 | ||
10 | 1/3/2023 | T04 | 647.5 | 15.63 | ||
Source |
Ideally, I would like for the macro to be able to recognize that rows 5 and 6 occurred on the same "Date" and also have the same "Code" and the sum of the values on Column C(33.15 & 10.12) add up to 43.27 which is a value on Column D. So I would like for the final result to look something like this:
Book1 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Date | Code | Total | Outstanding | ||
2 | 1/1/2023 | T01 | 50.56 | 500.25 | ||
3 | 1/1/2023 | T02 | 30.32 | 58.43 | ||
4 | 1/1/2023 | T03 | 58.43 | 30.32 | ||
5 | 1/2/2023 | T04 | 33.15 | 289.65 | ||
6 | 1/2/2023 | T04 | 10.12 | 43.27 | ||
7 | 1/2/2023 | T05 | 15.63 | 300 | ||
8 | 1/2/2023 | T01 | 289.65 | 50.56 | ||
9 | 1/3/2023 | T06 | 500.25 | 600.5 | ||
10 | 1/3/2023 | T04 | 647.5 | 15.63 | ||
Source |
Bonus question: Is there a way of getting this Macro to not use White as one of the interior fill colors? Cells C9 & D2 have matching values and are filled with white. I would like to avoid that if possible.
Thank you.