Formula Help

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
961
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi everyone,

Need help with creating a formula:
Digits = 0 through 9
Cells P4 through R4 are the most recent digits Position 1, Position 2, Position 3

Trying to calculate how many rows back when all 3 digits 9, 5, 4 appeared.

So using the example below. The 5 & 9 (from cell range P4 through R4) appeared on line 1. The remaining digit 4 did not appear until line 5 (Cell range P9, Q9, R9)

So the formula needed would give me a value of "5"

So I'm trying to calculate when all 3 digits from P4, Q4, R4 appear from the previous rows.

Note: A cell range could have duplicate digits. For example P4, Q4, R4 could be 9 9 4 or 5 5 5
So even though the digits repeat I would need to count the 1st occurrence of the 1st duplicate digit, 2nd digit of the duplicate or 3rd digit of the duplicate.
So for example if Cell P4 = 9, Q4 = 9, R4 = 6. The result would be: 3.
1st digit 9 appeared on line 1 Cell R5), the second digit 9 appeared on line 2 (cell P6) and the digit 6 appeared on line 3.

Cell P4 = 9
Cell Q4 = 5
Cell R4 = 4

1 Cells P5,Q5,R5 = 3 5 9
2 Cells P6,Q6,R6 = 9 0 3
3 Cells P7,Q7,R7 = 3 6 5
4 Cells P8,Q8,R8 = 6 9 9
5 Cells P9,Q9,R9 = 4 6 3
6 Cells P10,Q10,R10 = 5 7 6
7 Cells P11,Q11,R11 = 4 4 7

Thank you in advance!!
 
Last edited:
Hi Eric,

I'm using the formula you provided and it works to perfection for 3 digits.
Using the same idea and calculating when just 1 digit appears instead of 3.

Also using the same idea and calculating when 2 digits appears instead of 3.

Have a nice weekend!!

Thanks,
Steve
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
This formula will tell you the offset where the first value appears:

=IFERROR(SMALL(IF(MMULT(--(COUNTIF(OFFSET(P5:R5,0,0,ROW(P5:P50)-ROW(P5)+1),P4:R4)>=1),{1;1;1}),ROW(P5:P50)-ROW(P5)+1),1),"Not enough found")

with CSE.

The formula that handles 2 items is much trickier due to possible duplicates, and the trick I used for 3 columns won't work for 2. If I figure it out, I'll let you know.
 
Upvote 0
You might be better off with a UDF (User-Defined Function). I wrote one up you can try. To give it a shot, open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. In the sheet that opens, paste this code:

Code:
Public Function HowFar(ByVal MyRange As Range, ByVal MyCount As Long)
Dim MyData As Variant, i As Long, j As Long, k As Long

    MyData = MyRange.Value
    For i = 2 To UBound(MyData)
        For j = 1 To 3
            For k = 1 To 3
                If MyData(i, j) = MyData(1, k) Then
                    MyCount = MyCount - 1
                    MyData(1, k) = "x"
                    If MyCount = 0 Then
                        HowFar = i - 1
                        Exit Function
                    End If
                    Exit For
                End If
            Next k
        Next j
    Next i
    
    HowFar = "Not enough found"
End Function
Press Alt-Q to close the editor. Now you can use the function as shown:

ABCKLMNOPQRSAAABACAD
31st match2nd match3rd match
46/26/200795456/26/2007954954115
56/25/200735926/25/2007359359112
66/24/200790396/24/2007903903129
76/23/200736536/23/2007365365123
86/22/200769996/22/20076996991913
96/19/200746396/19/2007463463129
106/19/200757656/19/2007576576145
116/19/200744796/19/2007447447189
126/18/200787226/18/2007872872112
136/17/200782236/17/2007822822123
146/16/200727656/16/2007276276125
156/15/20072536/15/200725025133
166/14/200768586/14/2007685685118
176/13/200786946/13/2007869869344
186/12/200732046/12/2007320320134
196/11/20077466/11/200774074118
206/10/200767446/10/2007674674234
216/9/200738986/9/2007389389346
226/8/200726256/8/2007262262115
236/7/200722486/7/2007224224338
246/6/200778556/6/2007785785245
256/5/20070146/5/2007999999457
266/4/200724766/4/2007247247556
276/3/2007603126/3/20076036031112
286/2/200735666/2/2007356356156
296/1/200768916/1/2007689689111
305/31/200796845/31/2007968968224
315/30/200771285/30/2007712712348
325/29/200769455/29/2007694694135
335/28/2007955125/28/2007955955358
345/27/200781345/27/2007813813134
355/26/200747395/26/2007473473119
365/25/200743955/25/2007439439125
375/24/200716375/24/2007163163347
385/23/2007485125/23/200748548534Not enough found
395/22/2007255/22/20072002115
405/21/20076265/21/200762062346
415/20/200793545/20/2007935935124
425/19/200788975/19/2007889889144
435/18/200793295/18/200793293222Not enough found
445/17/200771195/17/2007710713Not enough foundNot enough found
455/16/200752945/16/2007529529Not enough foundNot enough foundNot enough found
465/15/200788675/15/2007886886Not enough foundNot enough foundNot enough found

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet15

Worksheet Formulas
CellFormula
AA4=howfar($P4:$R50,1)
AB4=howfar($P4:$R50,2)
AC4=howfar($P4:$R50,3)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Excellent Eric.
Works to perfection.
Thank you so much.
Steve
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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