Option Explicit
Sub demo()
Dim ws(1 To 3) As Worksheet
Dim DC_Rng As Range, DB_Rng As Range
Dim lr As Long, c As Long, r As Long, clr As Long
Dim dc_sum As Long, db_Sum As Long
Dim res As Variant
Application.ScreenUpdating = False
Set ws(1) = Sheets("Main Stats")
Set ws(2) = Sheets("Defined Contribution DC")
Set ws(3) = Sheets("Defined Benefit DB")
With ws(2)
lr = .Cells(Rows.Count, "A").End(xlUp).Row
Set DC_Rng = .Range("A3:A" & lr)
End With
With ws(3)
lr = .Cells(Rows.Count, "A").End(xlUp).Row
Set DB_Rng = .Range("A3:A" & lr)
End With
With ws(1)
For c = 2 To 6 Step 2
dc_sum = 0: db_Sum = 0
lr = .Cells(Rows.Count, c).End(xlUp).Row
For r = 6 To lr
If IsEmpty(.Cells(r, c)) Then Exit For
res = Application.Match(.Cells(r, c), DC_Rng, 0)
If IsNumeric(res) Then
clr = ws(2).Cells(res, 1).Interior.Color
.Cells(r, c).Resize(, 2).Interior.Color = clr
dc_sum = dc_sum + .Cells(r, c + 1)
Else
res = Application.Match(.Cells(r, c), DB_Rng, 0)
If IsNumeric(res) Then
clr = ws(3).Cells(res, 1).Interior.Color
.Cells(r, c).Resize(, 2).Interior.Color = clr
db_Sum = db_Sum + .Cells(r, c + 1)
Else
MsgBox "Agreement Type " & .Cells(r, c) & " Not found"
End If
End If
Next r
.Cells(4, c) = dc_sum: .Cells(4, c + 1) = db_Sum
Next c
End With
Application.ScreenUpdating = True
End Sub
Also calculates totals in row 4
Any reason for not putting DC/DB in the same sheet (adding another column with DC or DB if required) ? This will enable a simple Conditional Formatting solution as per reply from TMS in post #2
Bookmarks