Sum the underlying values of Primary ID and Child
Sub Sum_Of_Primary_And_Child_Underlying_Values()
Dim Wkb As Workbook
Set Wkb = ActiveWorkbook
Dim Sh As Worksheet
Set Sh = Wkb.Sheets("Sheet2")
Dim R As Integer
If Sh.Range("G" & Rows.Count).End(xlUp).Row > 3 Then
Sh.Range("F4:G" & Sh.Range("G" & Rows.Count).End(xlUp).Row).ClearContents
End If
Dim PID As Integer, CID As Integer, OutputFormula As String
Dim ChildExits As String
Dim FindRng As Range
For R = 4 To Sh.Range("C" & Rows.Count).End(xlUp).Row
OutputFormula = ""
PID = Sh.Cells(R, 3).Value
OutputFormula = Sh.Cells(R, 4).Address(True, True)
ChildExits = "Yes"
CID = Sh.Cells(R, 5).Value
Do Until ChildExits = ""
'If primary and child value having equivalent value
If PID = CID Then
Exit Do
End If
Set FindRng = Sh.Range(Cells(3, 3), Cells(Sh.Range("C4").End(xlDown).Row, 3)).Find(What:=CID, _
After:=Sh.Range("C3"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not FindRng Is Nothing Then
ro = FindRng.Row
OutputFormula = OutputFormula & "+" & Sh.Cells(ro, 4).Address(True, True)
CID = Sh.Cells(ro, 5).Value
End If
If FindRng Is Nothing Then
ChildExits = ""
End If
Loop
Sh.Cells(R, 6).Value = "=" & OutputFormula
Sh.Cells(R, 7).Value = "=FORMULATEXT(F" & R & ")"
Next
Set Wkb = Nothing
Set Sh = Nothing
OutputFormula = ""
End Sub