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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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)
 

Forum statistics

Threads
1,141,587
Messages
5,707,258
Members
421,498
Latest member
matinebi

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
Top