How to find first occurance based on given condition

Manamana

New Member
Joined
Jul 12, 2011
Messages
4
I like to get the position of first non-zero number out of a data range like this:

0 0 0 0 5 4 0 6 0

I am after result of 5 through any formula. Please help!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
UDF.
Code:
[COLOR="Blue"]Function[/COLOR] NonZero(Str [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]With[/COLOR] CreateObject("VBScript.RegExp")
        .Pattern = "[1-9]"
        NonZero = .Execute(Str)(0).FirstIndex + 1
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
I like to get the position of first non-zero number out of a data range like this:

0 0 0 0 5 4 0 6 0

I am after result of 5 through any formula. Please help!

One of...

Control+shift+enter, not just enter:

=MATCH(TRUE,A2:I2>0,0)

=MATCH(1,IF(ISNUMBER(A2:I2),IF(A2:I2>0,1)),0)

The latter would be adequate if there are text-valued cells in the range of interest.

If there are also negative numbers...

=MATCH(1,1-(A2:I2=0),0)

=MATCH(1,IF(ISNUMBER(A2:I2),IF(1-(A2:I2=0),1)),0)
 
Upvote 0
Thanks, Guys. You are geniuses! I have tried all suggested methods. Apart from Aladin's solution, others all work well. Don't know why, with Aladin's solution, there is always #value error. Anyway, thank you all! This is my first post in this forum. So far so good. Will keep coming back...
 
Upvote 0
Thanks, Guys. You are geniuses! I have tried all suggested methods. Apart from Aladin's solution, others all work well. Don't know why, with Aladin's solution, there is always #value error. Anyway, thank you all! This is my first post in this forum. So far so good. Will keep coming back...

Funny...

You need to confirm the formulas I suggested with control+shift+enter, not just enter. Formulawise,
they offer you exactly what you need.
 
Upvote 0
Hi, Guys, I found when it comes to a data range starting with a non-zero data, match formula will result in the total number of the range.
I.e.
2 1 0 0 6 1 0
Match (0,A1:A7,0) = 7

Where I am looking for 1 as a result. How can I fix the problem?
 
Upvote 0
Hi Aladin

what is the reason of using Control+shift+enter when i use array formula, what is the difference between normal formula & array formula.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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