Extract a number before specific letters

Pank

New Member
Joined
Mar 10, 2023
Messages
7
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I am trying to pull the numbers out of rows of text. The number placement varies, although it is always before a "d" or "dpm" or "x" or "kt" or "km" text.

I've searched older postings and I think the most suitable for my case is the below (found in thread "How to extract a number before a specific word in a string of text")

=0+(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(A1,"d",REPT(" ",99)),99))," ",REPT(" ",99)),99)))

but can't find a way/don't have the knowledge to add the rest of the letters (dpm, x, kt, km)

Examples of text:

buy apples 20d november 2022
winter 2021 buy oranges 27dpm
10x box sell oranges autumn 2022
sell pears 120kt autumn 2022

The needed numbers to extract are 20, 27, 10, 120 respectively.

Thank you very much in advance for any help.
 
Both the function and formula that JEC provided seems to work perfectly on the sample data you provided:

View attachment 87261

So if something is not working, maybe you are not applying the solutions properly.

Regarding the function, you do not need to know the first thing about RegEx in order to use. Just paste his function into a new VBA module in your workbook, and then you can use it like any other function, i.e. place this formula in cell B2:
Excel Formula:
=jec(A2)
and copy down for the rest of the rows.

And the function works properly too.
If that is not returning the expected results, then I suspect that either:
- you have not updated the formula to look at the cell your data is in
or
- you have not represented your sample data accurately

If you cannot get it to work, please show us an example of it not working where we can see:
- the exact data
- the exact cell address your data is placed in
- the exact formula you are using
- the result you are getting
Hi Joe,

following your instructions, I managed to apply Jec's function and it works.
I really can't thank enough all members of this thread.
If it's not too much to ask, is there a way for the function to display no result, i.e. leave the cell as blank (null), in case there is no data in cell A1? Currently it displays zeros.

Thank you again
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
is there a way for the function to display no result
You could try this version.
You may want to include "dpm" for completeness but note that "dpm" is not required in the .Pattern line since any number followed by dpm would already get picked when looking for a number followed by d. (See ** note below)

VBA Code:
Function getnum(s As String) As Variant
  getnum = vbNullString
  With CreateObject("VBScript.RegExp")
    .Pattern = "\d+(?=d|x|kt|km)"
    If .Test(s) Then getnum = Val(.Execute(s)(0))
  End With
End Function

Pank.xlsm
AB
1buy apples 20d november 202220
2winter 2021 buy oranges 27dpm27
3No number to extract here 
4 
510x box sell oranges autumn 202210
6sell pears 120kt autudmn 2022120
Extract Number
Cell Formulas
RangeFormula
B1:B6B1=getnum(A1)


** That does beg the question though: Could your data have a number followed by d and any other letter(s) apart from "dpm"?
For example, could you have
winter 2021 buy oranges 27def
and, if so, what should the function return?
 
Upvote 0
Solution
You could try this version.
You may want to include "dpm" for completeness but note that "dpm" is not required in the .Pattern line since any number followed by dpm would already get picked when looking for a number followed by d. (See ** note below)

VBA Code:
Function getnum(s As String) As Variant
  getnum = vbNullString
  With CreateObject("VBScript.RegExp")
    .Pattern = "\d+(?=d|x|kt|km)"
    If .Test(s) Then getnum = Val(.Execute(s)(0))
  End With
End Function

Pank.xlsm
AB
1buy apples 20d november 202220
2winter 2021 buy oranges 27dpm27
3No number to extract here 
4 
510x box sell oranges autumn 202210
6sell pears 120kt autudmn 2022120
Extract Number
Cell Formulas
RangeFormula
B1:B6B1=getnum(A1)


** That does beg the question though: Could your data have a number followed by d and any other letter(s) apart from "dpm"?
For example, could you have
winter 2021 buy oranges 27def
and, if so, what should the function return?
Hi Peter,
no cases of number followed by letters except d or dpm. Thus, all works perfectly now.
I am truly grateful for all the help.
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)

no cases of number followed by letters except d or dpm.
I'm wondering then if the task could be simplified to find a number followed immediately by any (lower case) letter(s)?
Not that it would probably make a lot of difference but if that was the case you would not need to individually specify those following sets of characters in the pattern line.

VBA Code:
Function getnum(s As String) As Variant
  getnum = vbNullString
  With CreateObject("VBScript.RegExp")
'    .Pattern = "\d+(?=d|x|kt|km)"
    .Pattern = "\d+(?=[a-z])"
    If .Test(s) Then getnum = Val(.Execute(s)(0))
  End With
End Function
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)


I'm wondering then if the task could be simplified to find a number followed immediately by any (lower case) letter(s)?
Not that it would probably make a lot of difference but if that was the case you would not need to individually specify those following sets of characters in the pattern line.

VBA Code:
Function getnum(s As String) As Variant
  getnum = vbNullString
  With CreateObject("VBScript.RegExp")
'    .Pattern = "\d+(?=d|x|kt|km)"
    .Pattern = "\d+(?=[a-z])"
    If .Test(s) Then getnum = Val(.Execute(s)(0))
  End With
End Function

Thank you once more. Will implement the revised function.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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