Author: Oscar Cronquist Article last updated on January 21, 2023

Find a sequence of values2

This article demonstrates array formulas that identify two search values in a row or in a sequence. The image above shows two search values in cell E2 and E3, the formula in cell F2 looks for those search values in column A and the corresponding row numbers are returned if they match.

The order is important, meaning the search values must be in a sequence exactly as they are written in cell E2 and E3.

1. How to find two search values next to each other vertically?

Find a sequence of values1

The image above shows a formula in cell F2 that returns only the first match based on the search values in cell E2 and E3.

This array formula finds the sequence "Axis Chemical Co." (cell E2) and "Southern Railway" (cell E3) in two consecutive cells in column A and returns the row number in cell F2.

Array formula in cell F11:

=MATCH(E2&E3, $A$1:$A$23&$A$2:$A$24, 0)

Update 3-17-2021, the following Excel 365 formula extracts the corresponding values from the column B and C based on the found sequences:

=FILTER(B2:C23,(A2:A23=E2)*(A3:A24=E3))

How to use the FILTER function

Back to top

How to enter an array formula

There is no need to enter the formulas if you own Excel 365, it uses dynamic array formulas.

  1. Double press with left mouse button on cell F2. The prompt shows up.
  2. Paste formula to cell.
  3. Press and hold CTRL + SHIFT simultaneously.
  4. Press Enter once.
  5. Release all keys.

The formula now looks like this: {=MATCH(E2&E3, $A$1:$A$23&$A$2:$A$24, 0)}

Don't enter the beginning and ending curly brackets yourself, they appear automatically if you managed to enter the array formula successfully.

Back to top

Explaining formula in cell F2

Step 1 - Concatenate cells

The ampersand lets you concatenate cell values.

E2&E3

becomes

"Axis Chemical Co."&"Southern Railway"

and returns

"Axis Chemical Co.Southern Railway"

Step 2 - Concatenate cell ranges

The second argument in the MATCH function is $A$1:$A$23&$A$2:$A$24. The ampersand concatenates the two cell ranges into one. By concatenating a value with the value below you can easily search for a sequence of values.

$A$1:$A$23&$A$2:$A$24

becomes

{"Company";"Paradise Airlines";"LexCorp";"Uplink Corporation";"Massive Dynamic";"Paradise Airlines";"Bad Wolf Corporation";"Atlantic Northern";"Axis Chemical Co.";"Atlantic Northern";"Axis Chemical Co.";"Southern Railway";"Axis Chemical Co.";"Axis Chemical Co.";"Atlantic Northern";"Massive Dynamic";"LexCorp";"Paradise Airlines";"Southern Railway";"LexCorp";"Massive Dynamic";"Axis Chemical Co.";"Southern Railway"}&{"Paradise Airlines";"LexCorp";"Uplink Corporation";"Massive Dynamic";"Paradise Airlines";"Bad Wolf Corporation";"Atlantic Northern";"Axis Chemical Co.";"Atlantic Northern";"Axis Chemical Co.";"Southern Railway";"Axis Chemical Co.";"Axis Chemical Co.";"Atlantic Northern";"Massive Dynamic";"LexCorp";"Paradise Airlines";"Southern Railway";"LexCorp";"Massive Dynamic";"Axis Chemical Co.";"Southern Railway";0}

and returns

{"CompanyParadise Airlines";"Paradise AirlinesLexCorp";"LexCorpUplink Corporation";"Uplink CorporationMassive Dynamic";"Massive DynamicParadise Airlines";"Paradise AirlinesBad Wolf Corporation";"Bad Wolf CorporationAtlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern RailwayAxis Chemical Co.";"Axis Chemical Co.Axis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernMassive Dynamic";"Massive DynamicLexCorp";"LexCorpParadise Airlines";"Paradise AirlinesSouthern Railway";"Southern RailwayLexCorp";"LexCorpMassive Dynamic";"Massive DynamicAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern Railway"}

Step 3 - Find the relative position

The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.

MATCH(lookup_value, lookup_array, [match_type])

MATCH(E2&E3, $A$1:$A$23&$A$2:$A$24, 0)

becomes

MATCH("Axis Chemical Co.Southern Railway", {"CompanyParadise Airlines";"Paradise AirlinesLexCorp";"LexCorpUplink Corporation";"Uplink CorporationMassive Dynamic";"Massive DynamicParadise Airlines";"Paradise AirlinesBad Wolf Corporation";"Bad Wolf CorporationAtlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern RailwayAxis Chemical Co.";"Axis Chemical Co.Axis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernMassive Dynamic";"Massive DynamicLexCorp";"LexCorpParadise Airlines";"Paradise AirlinesSouthern Railway";"Southern RailwayLexCorp";"LexCorpMassive Dynamic";"Massive DynamicAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern Railway"}, 0)

and returns 11.

Back to top

2. How to find all instances of two search values next to each other vertically?

Find a sequence of values2

The image above demonstrates an array formula that returns row numbers of all found sequences based on the search values in cells E2 and E3.

Array formula in cell F2:

=SMALL(IF($E$2&$E$3=$A$1:$A$23&$A$2:$A$24, MATCH(ROW($A$2:$A$23), ROW($A$2:$A$23))), ROWS($A$1:A1))

Copy cell F2 and paste cells below as far as needed.

How to enter an array formula

Update 3-17-2021,the following Excel 365 formula extracts the corresponding values from the column B and C based on the found sequences:

=FILTER(B2:C23,(A2:A23=E2)*(A3:A24=E3))

How to use the FILTER function

Back to top

Explaining formula in cell F2

Step 1 - Logical expression

$E$2&$E$3=$A$1:$A$23&$A$2:$A$24

becomes

"Axis Chemical Co.Southern Railway"={"CompanyParadise Airlines";"Paradise AirlinesLexCorp";"LexCorpUplink Corporation";"Uplink CorporationMassive Dynamic";"Massive DynamicParadise Airlines";"Paradise AirlinesBad Wolf Corporation";"Bad Wolf CorporationAtlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern RailwayAxis Chemical Co.";"Axis Chemical Co.Axis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Atlantic NorthernMassive Dynamic";"Massive DynamicLexCorp";"LexCorpParadise Airlines";"Paradise AirlinesSouthern Railway";"Southern RailwayLexCorp";"LexCorpMassive Dynamic";"Massive DynamicAxis Chemical Co.";"Axis Chemical Co.Southern Railway";"Southern Railway"}

and returns

{FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; TRUE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; TRUE; FALSE}

Step 2 - Create a sequence of numbers from 1 to n

The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.

MATCH(lookup_value, lookup_array, [match_type])

MATCH(ROW($A$2:$A$23), ROW($A$2:$A$23)))

becomes

MATCH({2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23}, {2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23})

and returns

{1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22}

Step 3 - Evaluate IF function

The IF function returns the corresponding row number if the logical value is TRUE and FALSE if FALSE.

IF($E$2&$E$3=$A$1:$A$23&$A$2:$A$24, MATCH(ROW($A$2:$A$23), ROW($A$2:$A$23)))

becomes

IF({FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; TRUE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; TRUE; FALSE}, {1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22})

and returns

{FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; 11; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; 22; FALSE}

Step 4 - Find k-th smallest number in array

The SMALL function returns the k-th smallest value from a group of numbers. It ignores text and boolean values.

SMALL(array, k)

SMALL(IF($E$2&$E$3=$A$1:$A$23&$A$2:$A$24, MATCH(ROW($A$2:$A$23), ROW($A$2:$A$23))), ROWS($A$1:A1))

becomes

SMALL({FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; 11; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; 22; FALSE}, 1)

and returns 11.

Back to top

3. How to find a sequence with any value between?

Find a sequence of values3

The image above demonstrates a formula in cell F2 that looks for a sequence of three values where the middle one can be anything.

Array formula in cell F2:

=MATCH(E2&E4, $A$1:$A$23&$A$3:$A$25,0)

How to enter an array formula

Update 3-17-2021, the following Excel 365 formula extracts the corresponding values from the column B and C based on the found sequences:

=FILTER(B2:C23,(E2=A2:A23)*(E4=A4:A25))

Back to top

Explaining formula in cell F2

Step 1 - Concatenate lookup_value

Note that cells E2 and E4 are now being concatenated.

E2&E4

becomes

"Massive Dynamic"&"Southern Railway"

and returns

"Massive DynamicSouthern Railway"

Step 2 - Concatenate lookup_array

The lookup array consists of two cell ranges concatenated, note that the first one begins at $A$1 and the second one at cell $A$3.

$A$1:$A$23&$A$3:$A$25

returns

{"CompanyLexCorp";"Paradise AirlinesUplink Corporation";"LexCorpMassive Dynamic";"Uplink CorporationParadise Airlines";"Massive DynamicBad Wolf Corporation";"Paradise AirlinesAtlantic Northern";"Bad Wolf CorporationAxis Chemical Co.";"Atlantic NorthernAtlantic Northern";"Axis Chemical Co.Axis Chemical Co.";"Atlantic NorthernSouthern Railway";"Axis Chemical Co.Axis Chemical Co.";"Southern RailwayAxis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Axis Chemical Co.Massive Dynamic";"Atlantic NorthernLexCorp";"Massive DynamicParadise Airlines";"LexCorpSouthern Railway";"Paradise AirlinesLexCorp";"Southern RailwayMassive Dynamic";"LexCorpAxis Chemical Co.";"Massive DynamicSouthern Railway";"Axis Chemical Co.";"Southern Railway"}

Step 3 - Find the relative position of the lookup_value in the lookup_array

The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.

MATCH(lookup_value, lookup_array, [match_type])

MATCH(E2&E4,$A$1:$A$23&$A$3:$A$25,0)

becomes

MATCH("Massive DynamicSouthern Railway", {"CompanyLexCorp";"Paradise AirlinesUplink Corporation";"LexCorpMassive Dynamic";"Uplink CorporationParadise Airlines";"Massive DynamicBad Wolf Corporation";"Paradise AirlinesAtlantic Northern";"Bad Wolf CorporationAxis Chemical Co.";"Atlantic NorthernAtlantic Northern";"Axis Chemical Co.Axis Chemical Co.";"Atlantic NorthernSouthern Railway";"Axis Chemical Co.Axis Chemical Co.";"Southern RailwayAxis Chemical Co.";"Axis Chemical Co.Atlantic Northern";"Axis Chemical Co.Massive Dynamic";"Atlantic NorthernLexCorp";"Massive DynamicParadise Airlines";"LexCorpSouthern Railway";"Paradise AirlinesLexCorp";"Southern RailwayMassive Dynamic";"LexCorpAxis Chemical Co.";"Massive DynamicSouthern Railway";"Axis Chemical Co.";"Southern Railway"},0)

and returns 21.

If you examine this combined cell ref $A$1:$A$23&$A$3:$A$25 you can see that the first one starts at A1 and the second one at A3. This is because the second value in the sequence is a value that can be anything. Let us see the values behind these cell refs.

Cell reference $A$3:$A$24 is wrong in the image above, it should be $A$3:$A$25.

Back to top

Find multiple sequences with any value between

Find a sequence of values4

Array formula in cell F2:

=SMALL(IF($E$2&$E$4=$A$1:$A$23&$A$3:$A$25, MATCH(ROW($A$2:$A$23), ROW($A$2:$A$23))), ROW(A1))

How to enter an array formula

Update 3-17-2021, the following Excel 365 formula extracts the corresponding values from the column B and C based on the found sequences:

=FILTER(B2:C23,(E2=A2:A23)*(E4=A4:A25))

Back to top

Finding a sequence - potential problems

Find a sequence of values5
Array formula in cell F2:

=MATCH(E2&E3,B1:B23&B2:B24,0)

How to enter an array formula

As you can see, the formula returns row 2. Why is that? The MATCH function looks for the value in cell E2 and E3 combined like this, 53 and 89 becomes 5389. The values in cell B2 and B3 are 538 and 9, becomes 5389 and that is a match.

So if we add a delimiting character we can rule out these problems, see the next formula.

Back to top

Array formula in cell F6:

=MATCH(E2&"-"&E3,B1:B23&"-"&B2:B24,0)

How to enter an array formula

But to be really sure that this works you must check that the character "-" does not exist at all in column B or you could get same the error again. The next formula uses the COUNTIFS function and I think this is the best method, no need for delimiting characters.

Back to top

Array formula in cell F10:

=MATCH(1,COUNTIFS(E10,B1:B23,E11,B2:B24),0)

How to enter an array formula

The downside with this formula is that you can´t find multiple sequences in a column.

If you want to learn more about array formulas join Advanced excel course.

Back to top

Get the Excel file


Find-a-sequence.xlsx

Read more

Back to top