Trying to optimize this function to find numbers after a specific text

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

I found this formula online and adapted it to some extent, and I'm trying to figure out how it works, but also why I get some strange results.

My goal is to find and extract only the numbers that occur after a specific text in a string, in this case the word "day". So in this example, I need the output to be "1". I don't want the numbers that are further to the right of the numbers that are closest to "day". Here in the case of A8, I get a strange output, and wonder why.

Of course, if there are other better formulas, please let me know as well.

Book1
ABCD
1dfeDay1 gh23gew1
2Day 1 gh23gew451
3gejxqwqDay 1gh23gew1
4dfegejday 1 )gh23gew1
5dfegej32day 1 )gh23gew1
6dfegej32gh23gewDay 11
7dfegej32gh23gewDay 1a1
8j32gh23gewDay 1 an0.041667
9j32gh23gewDay 1an1
10dfeDay-1 gh23gew31
11dfeDay-1 -gh23gew1
12dfeDay-(1) -gh23gew1
13
Sheet1
Cell Formulas
RangeFormula
C1:C12C1=LOOKUP(10^6,1*MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789",SEARCH("Day",""&A1&""))),{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}))


Thanks for any input!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
With a string like this: j32gh23gewDay 1 an

Excel is treating 1*"1 a" as 1:00 am, i.e. a value of 1/24 = 0.041667.

Here's one way you could tweak your formula (only very lightly tested!):

=MAX(IFERROR(--MID(A1,MIN(IFERROR(FIND(SEQUENCE(10,,0),A1,SEARCH("Day",A1)),999)),SEQUENCE(LEN(A1))),0))
 
Upvote 0
I'm assuming that the other results in post 1 are what you want?
I wasn't sure since the description below could be interpreted as wanting "123" from the first example, or "1" and "23" since all 3 digits occur after "Day"
My goal is to find and extract only the numbers that occur after a specific text in a string, in this case the word "day".

Another option might also be to employ a user-defined function like this.

VBA Code:
Function FirstNums(sData As String, sLookfor As String) As Long
  With CreateObject("VBScript.RegExp")
    .IgnoreCase = True
    .Pattern = "(" & sLookfor & "\D*)(\d+)"
    FirstNums = .Execute(sData)(0).SubMatches(1)
  End With
End Function

Rnkhch.xlsm
ABC
1dfeDay1 gh23gew1
2Day 1 gh23gew451
3gejxqwqDay 1gh23gew1
4dfegejday 1 )gh23gew1
5dfegej32day 1 )gh23gew1
6dfegej32gh23gewDay 11
7dfegej32gh23gewDay 1a1
8j32gh23gewDay 1 an1
9j32gh23gewDay 1an1
10dfeDay-1 gh23gew31
11dfeDay-1 -gh23gew1
12dfeDay-(1) -gh23gew1
13abcDay - 123def123
Sheet1
Cell Formulas
RangeFormula
C1:C13C1=FirstNums(A1,"Day")
 
Upvote 0
Thank you Stephen for solving the mystery. And it seems that your function is working very well in all the tests that I did 🤗

And thank you Peter for the fabulous function! 🤗 (I only needed the numbers closer to "day" so your function does exactly what I needed.)

Now I'm thinking whose post should I mark as solution 😅
 
Upvote 0
Now I'm thinking whose post should I mark as solution 😅
It is not important which one you mark, but it would be good if you could mark one of them. You could take Stephen's suggested approach or perhaps mark the one that you ended up using in your project. :)
 
Upvote 0
I used Stephen's formula as I thought people at work may have an easier time understanding it rather than a VBA module. Also he's far behind you in solutions count lol 😅
 
Upvote 0
Alternative way for whom with Ex2016 or erlier (without SEQUENCE function)

Code:
=AGGREGATE(14,6,MID(MID(A1,SEARCH("Day",A1)+3,255),AGGREGATE(15,6,SEARCH({1,2,3,4,5,6,7,8,9},MID(A1,SEARCH("Day",A1)+3,255)),1),{1,2,3,4,5})+0,1)
 
Upvote 0
I used Stephen's formula as I thought people at work may have an easier time understanding it
Fair enough & a good reason.

Just been thinking about it a bit more though, and not knowing anything about your real data, all the formula methods (so far at least) could give you some unexpected results I think - see below.
I have suggested another formula solution in column G that should eliminate such a possibility.

Rnkhch.xlsm
ABCDEFG
1Post #:31289
2abcday1e2f3g11001001001
3abcday12dec24gh1245638456384490712
Sheet1
Cell Formulas
RangeFormula
C2:C3C2=FirstNums(A2,"Day")
D2:D3D2=LOOKUP(10^6,1*MID(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789",SEARCH("Day",""&A2&""))),{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}))
E2:E3E2=MAX(IFERROR(--MID(A2,MIN(IFERROR(FIND(SEQUENCE(10,,0),A2,SEARCH("Day",A2)),999)),SEQUENCE(LEN(A2))),0))
F2:F3F2=AGGREGATE(14,6,MID(MID(A2,SEARCH("Day",A2)+3,255),AGGREGATE(15,6,SEARCH({1,2,3,4,5,6,7,8,9},MID(A2,SEARCH("Day",A2)+3,255)),1),{1,2,3,4,5})+0,1)
G2:G3G2=LET(r,REPLACE(A2,1,SEARCH("day",A2),""),ch,MID(r,SEQUENCE(LEN(r)),1),LEFT(SUBSTITUTE(TRIM(CONCAT(IF(ISNUMBER(ch+0),ch," ")))," ",REPT(" ",20)),20)+0)
 
Upvote 0
Solution
This is an incredible analysis and very informative and educational; thank you! 🤗 Both your functions are rock solid. You are an absolute genius 🧠

I'll mark it as solution 🍻
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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