1st occurance of non blank cell

gaz_chops

Well-known Member
Joined
Apr 29, 2003
Messages
6,485
Platform
  1. MacOS
I have data across rows in which I want to find the 1st occurance of a non blank cell and return this as a reference.
The formula in col A was kindly provided by Aladin, this finds the last entry, can this be tweaked? I'll be honest and say that I dont't quite understand how it works.

Row 5 should return 2
Row 9 should return 29
Book2
ABCDEFGHIJK
2F2000P07F2000P08F2000P09F2000P10F2000P11F2000P12F2001P01F2001P02F2001P03F2001P04
34122941
4282928
59339
62319352927353323
Sheet1
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi gazchops,

This user defined function will do it (until Aladin comes along with a non-VBA solution),
Code:
Function firstocc(rng As Range)

Dim cl As Range

firstocc = 0#

For Each cl In rng
    If Not IsNull(cl) And cl <> "" Then
        firstocc = cl.Value
        Exit Function
    End If
Next cl

firstocc = cl.Value
 
End Function

Just paste the above code into a standard module, and use the formula firstocc() like any other built-in Excel function.

In your case, it would be like = firstocc(B3:IV3) in row 3

HTH,
 
Upvote 0
That formula for the last non-zero numeric value must be...

=INDEX(B3:IV3,,MAX(INDEX((B3:IV3<>0)*(B3:IV3<>"")*COLUMN(B3:IV3),1,0))-COLUMN(B3:IV3)+1)

For the first value (any value)...

=INDEX(B3:IV3,MATCH(1,INDEX(1-ISBLANK(B3:IV3),1,0),0))
Book2
ABCDEFGHIJKL
1
2F2000P07F2000P08F2000P09F2000P10F2000P11F2000P12F2001P01F2001P02F2001P03F2001P04
322941
42928
5339
619352927353323
7
8241
92928
10339
111923
12
Sheet1
 
Upvote 0
Aladin, I don't know why but when I've pasted your formula it doesn't appear to work correctly.
I have added an extra bit to return the column header based on the ref the formula returns.
I also came up with a version. having tweaked your original formula, but this does not always work either.
Book1
ABCDEFGHIJ
2AladinInstallPdF2002P03F2002P04F2002P05F2002P06F2002P07F2002P08F2002P09F2002P10
30F2002P0428352128323128
4F2002P06F2002P09421
5F2002P10F2002P108
60F2002P0428352828402328
7F2003P10F2002P11
8F2003P06F2002P061628362728
90F2002P0428352531342928
100F2002P0430353228362728
110F2002P0428352828362621
120F2002P0734333029
130F2002P0428352828323128
140F2002P12
150F2002P092616
16F2002P10F2002P108
Sheet1
 
Upvote 0
The formula for the first value should work as intended...
Book13
ABCDEFGHIJK
1
2InstallPdF2002P03F2002P04F2002P05F2002P06F2002P07F2002P08F2002P09F2002P10
32F2002P0428352128323128
47F2002P09421
58F2002P108
62F2002P0428352828402328
7#N/A#N/A
84F2002P061628362728
92F2002P0428352531342928
102F2002P0430353228362728
112F2002P0428352828362621
125F2002P0734333029
132F2002P0428352828323128
14#N/A#N/A
152F2002P042616
168F2002P108
17
Sheet1


The formula...

=INDEX($C$2:$IV$2,MATCH(1,INDEX(1-ISBLANK(C3:IV3),1,0),0))

does not mirror the conditions incorporated in the formula for the last value. If that should be the case, we can expand the formula in question.

In column A you see the MATCH bit. If that is not necessary, ditch column A. The formula in column B retrieves the header that corresponds to the first value.
 
Upvote 0
The following array formula can be entered on the first cell of each row to obtain the first non-blank cell in each row from Column B to Column IV:

{=ADDRESS(ROW($A1),MIN(IF(NOT(ISBLANK($B1:$IV1)),COLUMN($B1:$IV1))))}
 
Upvote 0
Dear Aladin,

I am reading Mr Excel’s new book and the first formula is of course from you! (That is why I am posting here six years after the last post…)

Your formula is:

=INDEX(B3:IV3,MATCH(1,INDEX(1-ISBLANK(B3:IV3),1,0),0))

Then I tried this:

=INDEX(B3:IV3,MATCH(1,1-ISBLANK(B3:IV3),0))

Then I tried this one:

=INDEX(B3:IV3,MATCH(1,--(B3:IV3<>""),0))

All three seem to work.

When I look at these parts of the formulas:

INDEX(1-ISBLANK(B3:IV3),1,0) and 1-ISBLANK(B3:IV3) and ,--(B3:IV3<>"") I get the same column array of zeros and ones.

My questions are:

1) Why did you use the INDEX function and a 0 (zero) for the column_num?
2) Is any one of these formula parts faster calculating or more efficient in some way?
 
Upvote 0
Dear Aladin,

I am reading Mr Excel’s new book and the first formula is of course from you! (That is why I am posting here six years after the last post…)

Your formula is:

=INDEX(B3:IV3,MATCH(1,INDEX(1-ISBLANK(B3:IV3),1,0),0))

Then I tried this:

=INDEX(B3:IV3,MATCH(1,1-ISBLANK(B3:IV3),0))

Then I tried this one:

=INDEX(B3:IV3,MATCH(1,--(B3:IV3<>""),0))

All three seem to work.

When I look at these parts of the formulas:

INDEX(1-ISBLANK(B3:IV3),1,0) and 1-ISBLANK(B3:IV3) and ,--(B3:IV3<>"") I get the same column array of zeros and ones.

My questions are:

1) Why did you use the INDEX function and a 0 (zero) for the column_num?
2) Is any one of these formula parts faster calculating or more efficient in some way?

[A]

=INDEX(B3:IV3,MATCH(1,1-ISBLANK(B3:IV3),0))



=INDEX(B3:IV3,MATCH(1,--(B3:IV3<>""),0))

or better:

=INDEX(B3:IV3,MATCH(TRUE,B3:IV3<>"",0))

All of these requires CSE. These two are not equivalent. With [A], we would get a hit when B3 for example would house a formula blank like ="", with we wouldn't.

I thought I'll invent a non-CSE version (I wouldn't be surprised if somebody else also had the same idea before I did)...

=INDEX(B3:IV3,MATCH(1,INDEX(1-ISBLANK(B3:IV3),1,0),0))

This is equivalent to [A]. Since INDEX is also capable to return an array, the inner INDEX call used for that purpose:

INDEX(1-ISBLANK(B3:IV3),1,0)

means: All cells from row 1. The 0 bit specifies all columns
[*]. Not that this is an array of 1/0's. The Match bit looks up 1 in that array.


While this approach avoids CSE, its CSE equivalent [A] should be a bit less expensive.

------------------------

[*] Another example:

=INDEX(A2:B4,0,2)

which means: all cells from column 2, that is, B2, B3, and B4.
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,421
Members
452,514
Latest member
cjkelly15

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