Formula to count how many adjacent cells have value that precede specified cell

lisburl

New Member
Joined
Apr 7, 2018
Messages
8
Good afternoon!

I need help with a formula that will give me the total the number of cells in a row that contain a value that are adjacent to a target cell (H).

For example: Column H would total the following
<style type="text/css"><!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--></style>
BCDEFGH TOTAL
0001011
1101011
0101011
0011001
0101000
0100112
0001011
0101011
0101113
0011000

<colgroup><col style="width: 100px"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"><col width="100"></colgroup><tbody>
</tbody>


Thank you for any help.
 
Oops! Yes - you are correct. This is why I need a formula!! When I try to do this manually I make mistakes :)

BCDEFGH TOTAL
0001011
1101011
0101011
0011000
0101000
0100112
0001011
0101011
0101113
0011000

<tbody>
</tbody>

Maybe the formula below in H2 and copy down can helps:

=COLUMNS(B2:G2)-MATCH(2,INDEX(1/(B2:G2=0),),1)

Or

=COLUMNS(B2:G2)-MATCH(2,INDEX(1/(B2:G2=0),))

ABCDEFGHI
1Data01Data02Data03Data04Data05Data06Result
20001011
31101011
40101011
50011000
60101000
70100112
80001011
90101011
100101113
110011000
12
********************************************************

<tbody>
</tbody>


By the way, Rick's formula works for me.

Markmzz
 
Last edited:
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe the formula below in H2 and copy down can helps:

=COLUMNS(B2:G2)-MATCH(2,INDEX(1/(B2:G2=0),),1)

Or

=COLUMNS(B2:G2)-MATCH(2,INDEX(1/(B2:G2=0),))
You will need to put an IFERROR trap on your formulas like I did on mine in order to handle the case when all of the cells have 1's in them.
 
Upvote 0
You will need to put an IFERROR trap on your formulas like I did on mine in order to handle the case when all of the cells have 1's in them.

Thanks Rick.

=COLUMNS(B2:G2)-IFERROR(MATCH(2,INDEX(1/(B2:G2=0),)),0)

Markmzz
 
Last edited:
Upvote 0
Ricks formula works for me


Mark and Rick your formulas are playing with my head a bit

I understand how INDEX(1/(B2:G2=0),) is evaluating to {1,#Div/0,1,#Div/0,#Div/0,#Div/0} (or some varation of)
Is the match(2,{1,#Div/0,1,#Div/0,#Div/0,#Div/0}) part looking for 2 but can't find it so it returns the position
of the last valid number it located?

I might be cheating but here is what I would do if it crossed my desk

Public Function NearestRunones(ByVal rngRun As Range)
Dim strRange As String
Dim Cell As Range
For Each Cell In rngRun
strRange = strRange & Cell.Value2
Next Cell
strRange = StrReverse(strRange)
If InStr(strRange, 0) > 0 Then
NearestRunones = InStr(strRange, 0) - 1
Else
NearestRunones = 0
End If

End Function
 
Upvote 0
Ricks formula works for me


Mark and Rick your formulas are playing with my head a bit

I understand how INDEX(1/(B2:G2=0),) is evaluating to {1,#Div/0,1,#Div/0,#Div/0,#Div/0} (or some varation of)
Is the match(2,{1,#Div/0,1,#Div/0,#Div/0,#Div/0}) part looking for 2 but can't find it so it returns the position
of the last valid number it located?

I might be cheating but here is what I would do if it crossed my desk

Public Function NearestRunones(ByVal rngRun As Range)
Dim strRange As String
Dim Cell As Range
For Each Cell In rngRun
strRange = strRange & Cell.Value2
Next Cell
strRange = StrReverse(strRange)
If InStr(strRange, 0) > 0 Then
NearestRunones = InStr(strRange, 0) - 1
Else
NearestRunones = 0
End If
End Function
Your function incorrectly reports 0 if all the cells are filled with 1's.

If we assume the range is alway a single row horizontal range (as in the OP's case), your function can be rewritten as a one-liner...
Code:
[table="width: 500"]
[tr]
	[td]Function NearestRunones(ByVal rngRun As Range) As Long
  NearestRunones = Len(Split(StrReverse(Join(Application.Index(rngRun.Value, 1, 0), "")), 0)(0))
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Mark and Rick your formulas are playing with my head a bit

I understand how INDEX(1/(B2:G2=0),) is evaluating to {1,#Div/0,1,#Div/0,#Div/0,#Div/0} (or some varation of)
Is the match(2,{1,#Div/0,1,#Div/0,#Div/0,#Div/0}) part looking for 2 but can't find it so it returns the position
of the last valid number it located?

Hi Immdav!

Yes, you're right. The match(2,{1,#Div/0,1,#Div/0,#Div/0,#Div/0}) part is looking by a largest value that is less than or equal to 2 but it can't find it.
So it returns the position of the last value (errors are ignoreted) that is less than 2 (the last 1).

I hope that this helps.

Markmzz
 
Upvote 0
Markmzz,

Thanks for the explanation.

Rick,

cleaver use of the index and split function. Anything to avoid a loop....

David
 
Upvote 0
Rick,

cleaver use of the index and split function. Anything to avoid a loop....
It was more my penchant for compact code and one-liners (and it is so much better when the two combine) than it was to avoid a loop. :devilish:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,182
Messages
6,129,369
Members
449,506
Latest member
nomvula

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