Loop through an array

breakitdown

New Member
Joined
Sep 30, 2006
Messages
2
I have a single column arrary about 16 cells or so, say A1:A16. There are numerical values in each cell. I need either a vba procedure or user defined function that will return the position in the array once a sum is reached. For example if the values were (2,4,5,6,3,4) and my goal was to find "11" i want the procedure to return 3 which is the position in the array where 11 is reached

this is urgent, please help
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
A user defined function

Code:
Function fnd(aRange As Range, Target)
Dim tot, i, cel As Range
tot = 0
fnd = 0
i = 0
For Each cel In aRange
i = i + 1
tot = tot + cel.Value
If tot = Target Then
fnd = i
Exit Function
End If
Next cel
End Function

example: =fnd(A1:A6,11)
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top