Find a substring within a range and return the full string.

CKsteveFFF

New Member
Joined
Nov 13, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Given three variables (starting row, ending row, and substring) return the full string of the first cell found in the given range that contains the substring.

What formula can I use in D7 to return the string found within the given rows?
Looking to be able to change the rows as needed. For example, if the RowStart were changed to 1, it would return BAT1 since the range now includes another BAT further up.

I have toyed with various MATCH, SEARCH, VLOOKUP etc, but the variable range is throwing me.

1699924355508.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the Board!

If you use a helper column (column B) that returns the row numbers, here is one way:
Excel Formula:
=FILTER(A1:A15,(B1:B15>=D3)*(B1:B15<=D4)*(LEFT(A1:A15,LEN(D5))=D5))

1699963993907.png
 
Upvote 0
Welcome to the MrExcel board!

I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

You could try something like this without the helper column

Excel Formula:
=LET(r,A1:A15,INDEX(FILTER(r,ISNUMBER(SEARCH(D5,r))*(ROW(r)>=D3)*(ROW(r)<=D4),""),1))
 
Last edited:
Upvote 1
Welcome to the MrExcel board!

I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

You could try something like this without the helper column

Excel Formula:
=LET(r,A1:A15INDEX(FILTER(r,ISNUMBER(SEARCH(D5,r))*(ROW(r)>=D3)*(ROW(r)<=D4),""),1))
Peter, I think there is a typo in your formula. There should be a comma before the INDEX function, i.e.
Excel Formula:
=LET(r,A1:A15,INDEX(FILTER(r,ISNUMBER(SEARCH(D5,r))*(ROW(r)>=D3)*(ROW(r)<=D4),""),1))
 
Upvote 0
Peter, I think there is a typo in your formula.
Yes, thanks Joe. (y)
I edited the formula after posting and didn't realise that I had accidentally deleted the comma. I have put it back in now.
 
Upvote 0
Welcome to the MrExcel board!

I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

You could try something like this without the helper column

Excel Formula:
=LET(r,A1:A15,INDEX(FILTER(r,ISNUMBER(SEARCH(D5,r))*(ROW(r)>=D3)*(ROW(r)<=D4),""),1))

Thank you. A helper column in this case was perfectly suitable.
I'm currently unable to use XL2BB on this computer, but will likely post from another computer that I can use it on when I need to ask another question.

It has worked very well with my actual data set.
The only exception is if it finds multiple identical matches, it returns an array giving me a #SPILL error.
How can I make it return only the single result?
 
Upvote 0
The only exception is if it finds multiple identical matches, it returns an array giving me a #SPILL error.
How can I make it return only the single result?
You get a SPILL error when it will return multiple results, but you have something in the cell below it, so it is unable to "spill" over into the cells below to return the other results.

If you only want to return one single result, if multiple records match the conditions, how do you know which one it should return?
 
Upvote 0
You get a SPILL error when it will return multiple results, but you have something in the cell below it, so it is unable to "spill" over into the cells below to return the other results.

If you only want to return one single result, if multiple records match the conditions, how do you know which one it should return?

When it finds multiple matches, it returns an array with all of the matches in it. All of the array's contents are identical. Just taking the first row of the array would be fine.
 
Upvote 0
Did you try Peter's formula?
It seems to only return the first match for me when I tested it.
 
Upvote 0
=LET(r,OFFSET(INDEX(A:A,D3),,,D4-D3+1),INDEX(r,MATCH(D5&"*",r,0)))

substring.xlsx
ABCD
1CAT4
2DOG1Givens
3BAT1RowStart6
4COW6RowEnd10
5OWL8SubStringBAT
6CAT3
7DOG5ResultBAT9
8BAT9
9COW8
10OWL4
11CAT1
12DOG6
13BAT1
14COW4
15OWL2
Sheet1
Cell Formulas
RangeFormula
D7D7=LET(r,OFFSET(INDEX(A:A,D3),,,D4-D3+1),INDEX(r,MATCH(D5&"*",r,0)))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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