Find what numbers add up to a specific number

H

heathersm1

Hello,

I would like to know if there is a function which I could use to search a
column to find values which would equal a certain value.

For instance I have a list of:

1
2
8
98
65
45
25
98

I would like to search that column for any of those values which would add
to 123. The function would then return or highlight the 98 and 25 because
those add up to 123.

Is there a way I can do this?

Thanks for your help!
 
J

Jacob Skaria

Try the below code

HighlightParent 500, 1
500 is the sum
1 is the column number

Sub HighlightParent(lngSum As Long, lngCol As Long)
Dim lngRow, lngRows, lngTRow
lngRows = ActiveSheet.Cells(Rows.Count, lngCol).End(xlUp).Row
For lngRow = 1 To lngRows
For lngTRow = lngRow To lngRows
If Cells(lngRow, lngCol) + Cells(lngTRow, lngCol) = lngSum Then
Cells(lngRow, lngCol).Interior.Color = vbYellow
Cells(lngTRow, lngCol).Interior.Color = vbYellow
Exit Sub
End If
Next
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top