Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sum the results of a loop in columns and rows of numbers I have a loop that sear

ID: 3571102 • Letter: S

Question

Sum the results of a loop in columns and rows of numbers

I have a loop that searches across column headers by criteria. For example, the word "Green" might be the top column headers of 8 or 10 columns. The column headers may not always next to each other. I want to SUM the value in each row in a distant column when ever a value in the column matches the criteria.

For r = 2 To 500
For c = 3 To 52
If Cells(1, c).Value = "GREEN" Then
GreenVAL = Cells(r, c).Value
Debug.Print GreenVAL
End If
Next c
Next r

When I use Debug.Print GreenVal on a single row (like For r = 6 to 6) the Immediate Window shows that the correct values were found. I'm just not sure how to add them all together. I would be grateful for your help !

Thanks !

Explanation / Answer

Hi..

I figured it out:

For c = 3 To 52
If Cells(1, c).Value = "GREEN" Then
GreenVAL = GreenVAL + Cells(r, c).Value
Cells(r, 56).Value = GreenVAL
End If
Next c
GreenVAL = 0
Next r

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote