Extract Number from Alphanumeric String

dwgnome

Active Member
Joined
Dec 18, 2005
Messages
441
Looking for assistance in figuring out the best formula to extract the VOC content value from variable length alphanumeric string. The VOC content is the number just before the "g/L".

I need to extract just the number portion just before the units “g/L”. Most of the time there is a space before the number and after the number and before “g/L”. However, there are times when there may not be any spaces. In either case, I would like to extract the numbers with decimals if it has any.

See the following examples:


Excel Workbook
D
1SEALANT-RETAINING COMPOUND SINGLE COMPONENT ANAEROBIC, SCR40372A, *Loctite 242, 10 ML BOTTLE,146 g/L VOC
2RESIN-EPOXY ALUMINUM FILLED 4 OUNCE PATCH KIT\ * * HYSOL 6C10 g/L VOC
3SEALANT-THREAD LOCK *HIGH STR *MIL-S-22473 *GRADE AA *50 ML BOTTLE * * *SCR802269A\ * Loctite 089 Weld Sealant125.3 g/L VOC
4SEALANT-RETAINING COMPOUND ANAEROBIC MIL-S-22473 * * *GRADE C 50 ML BOTTLE * *SCR802266A\ *Loctite 84 * 167.4 g/L VOC
5RESIN-LAMINATING SYSTEM FOR REPAIR OF 250/350 DEG F CURING ADVANCED COMPOSITES ONE QUART KIT\ * EPOCAST 35A *12 g/L VOC
6CLEANER-THRUST REVERSER 55 GAL DRUM\ *TURCO 5805 * *0 g/L VOC
7SOLVENT-TETRAHYDROFURAN REAGENT GRADE IN 4-LITER * * JT Baker JT9450-5 * * 887 g/L VOC
8CLEANER-AIRCRAFT PARTS * HOT TANK * UNTHICKENED *55 GALLON DRUM\ * CALA 805 *25g/L VOC
9CLEANER\ * Fine Organics FO-585 * * *0 g/L VOC
10SEALANT-THREAD LOCK HIGH STRENGTH MIL-S-22473 *GRADE AV, *50 ML BOTTLE, *SCR802270A, 175.2 g/L VOC
11RESIN-EPOXY FIBERGLASS SELF EXTINGUISHING KIT BMS8-201TYII, *1 Qt Kit, *Epocast 50-A1, * *357 g/L VOC
12CLEANER-AIRCRAFT INTERIOR UTILITY 55 GAL DRUM\ * *AEROCLEAN X-410Q * *20 g/L VOC
13RESIN-PASTE PINHOLE FILLER FOR RADOMES 50 GRAM KIT\ * * * EPIBOND 156 A/B * * 10 g/L VOC
14SEALANT-RETAINING COMPOUND ANAEROBIC MIL-R-46082 TY 1 *250 ML BOTTLE * *SCR18016A\ * Loctite 675 141.7g/L *VOC
15RESIN-EPOXY CLEAR 3.35 OUNCE PATCH KIT\ * * *Hysol 0151 *Chemical Database uses UMS *ADH3002-96 * 34. g/L *VOC
Sheet1


The results I am looking for:


Excel Workbook
H
1146
210
3125.3
4167.4
512
60
7887
825
90
10175.2
11357
1220
1310
14141.7
1534
Sheet1


Any help would be appreciated.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi dwgnome,

Based on your data, I don't think there is an excel formula (or combination of formulas) to do the job :(

That said, I’ve written the following User Defined Function (udf) to do extract a number from the right of a string (doesn't include negative numbers):

Code:
Function MyNumberExtract(rngCell As Range) As Currency

    Dim intChrCnt As Integer
    Dim varTempString As Variant
    
     'Searches the string from right to left, but puts the result left to right
     For intChrCnt = Len(rngCell) To 1 Step -1
        'If the current character is either numeric or a decimal point, then...
        If IsNumeric(Mid(rngCell, intChrCnt, 1)) = True Or _
           Mid(rngCell, intChrCnt, 1) = "." Then
                '...add to the 'varTempString' variable
                varTempString = Mid(rngCell, intChrCnt, 1) & varTempString
                'If the current character is not numeric or a decimal point and _
                'varTempString' variable is populated, then...
                ElseIf varTempString <> "" Then
                    '...exit the loop.
                    Exit For
                End If
    Next intChrCnt
    
    'Convert the 'varTempString' variable to a currency variable.
    MyNumberExtract = CCur(varTempString)
       
End Function

To insert the code, follow these five steps:

1. Copy (Ctrl + C) my code
2. Open the VBA editor (Alt + F11)
3. From the Insert menu click Module
4. Paste (Ctrl + V) my code from step 1 above into the blank module
5. From the File menu click Close and Return to Microsoft Excel

Then use the udf as you would any other native excel formula - i.e. =MyNumberExtract(D1)
 
Upvote 0
Hi

Using formula (although I suspect there is a neater alternative available):

First select the range with the text and do 2 x Find&Replace.
1.
Find: [space]g/L
Replace with: g/L
2.
Find: g/L
Replace with: [space]g/L

The find&amp;replace is so that we consistently apply a space before each occurence of g/L.

Now the following formula solution, example below:
Excel Workbook
CDE
1SEALANT-RETAINING COMPOUND SINGLE COMPONENT ANAEROBIC, SCR40372A, *Loctite 242, 10 ML BOTTLE,146 g/L VOC146
2RESIN-EPOXY ALUMINUM FILLED 4 OUNCE PATCH KIT\ * * HYSOL 6C10 g/L VOC10
3SEALANT-THREAD LOCK *HIGH STR *MIL-S-22473 *GRADE AA *50 ML BOTTLE * * *SCR802269A\ * Loctite 089 Weld Sealant125.3 g/L VOC125.3
4SEALANT-RETAINING COMPOUND ANAEROBIC MIL-S-22473 * * *GRADE C 50 ML BOTTLE * *SCR802266A\ *Loctite 84 * 167.4 g/L VOC167.4
5RESIN-LAMINATING SYSTEM FOR REPAIR OF 250/350 DEG F CURING ADVANCED COMPOSITES ONE QUART KIT\ * EPOCAST 35A *12 g/L VOC12
6CLEANER-THRUST REVERSER 55 GAL DRUM\ *TURCO 5805 * *0 g/L VOC0
7SOLVENT-TETRAHYDROFURAN REAGENT GRADE IN 4-LITER * * JT Baker JT9450-5 * * 887 g/L VOC887
8CLEANER-AIRCRAFT PARTS * HOT TANK * UNTHICKENED *55 GALLON DRUM\ * CALA 805 *25 g/L VOC25
9CLEANER\ * Fine Organics FO-585 * * *0 g/L VOC0
10SEALANT-THREAD LOCK HIGH STRENGTH MIL-S-22473 *GRADE AV, *50 ML BOTTLE, *SCR802270A, 175.2 g/L VOC175.2
11RESIN-EPOXY FIBERGLASS SELF EXTINGUISHING KIT BMS8-201TYII, *1 Qt Kit, *Epocast 50-A1, * *357 g/L VOC357
12CLEANER-AIRCRAFT INTERIOR UTILITY 55 GAL DRUM\ * *AEROCLEAN X-410Q * *20 g/L VOC20
13RESIN-PASTE PINHOLE FILLER FOR RADOMES 50 GRAM KIT\ * * * EPIBOND 156 A/B * * 10 g/L VOC10
14SEALANT-RETAINING COMPOUND ANAEROBIC MIL-R-46082 TY 1 *250 ML BOTTLE * *SCR18016A\ * Loctite 675 141.7 g/L *VOC141.7
15RESIN-EPOXY CLEAR 3.35 OUNCE PATCH KIT\ * * *Hysol 0151 *Chemical Database uses UMS *ADH3002-96 * 34. g/L *VOC34
new
Cell Formulas
RangeFormula
E1=--MID(C1,MAX(IF(ISERROR(--MID(SUBSTITUTE(C1,".","0"),ROW(INDIRECT("1:"&FIND("g/L",C1)-2)),1)),ROW(INDIRECT("1:"&FIND("g/L",C1)-2))))+1,FIND("g/L",C1)-2-MAX(IF(ISERROR(--MID(SUBSTITUTE(C1,".","0"),ROW(INDIRECT("1:"&FIND("g/L",C1)-2)),1)),ROW(INDIRECT("1:"&FIND("g/L",C1)-2)))))
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Last edited by a moderator:
Upvote 0
Hi dwgnome

Assuming numbers with no more than 6 characters, try in H1:

=-LOOKUP(1,-RIGHT(TRIM(LEFT(D1,FIND("g/L",D1)-1)),{1,2,3,4,5,6}))

Copy down
 
Upvote 0
Hi dwgnome

Assuming numbers with no more than 6 characters, try in H1:

=-LOOKUP(1,-RIGHT(TRIM(LEFT(D1,FIND("g/L",D1)-1)),{1,2,3,4,5,6}))

Copy down

Wow - this is neat (y)... Don't think I've ever noticed this method before :)
 
Upvote 0
Thank you all for your great contributions. :)

Trebor, yours worked the best. Only thing, when there is no number before the g/L or if no presence of g/L or VOC, your UDF catches the last existing number within the string. Is there a way to have the results show zero or blank in those cases?

Jon, yours works perfectly, but only after adding the spaces before and after the g/L. However, if there is no g/L or if no number, I get #VALUE! instead of blank or zero.

pgc01, yours is the coolest. :cool: Not sure what it is doing with the minus signs. Only thing is that it produces #NA instead of zero or blank when no number or g/L is present.


Is it possible to get each of these options to show zero or blank if no VOC related number is present? I have to have a number/blank for the output will be part of additional computations.

Thanks.
 
Upvote 0
I'm not even going to try and compete with Pedro's solutions, so I'll just nick it :biggrin:

=IF(ISNA(-LOOKUP(1,-RIGHT(TRIM(LEFT(D1,FIND("g/L",D1)-1)),{1,2,3,4,5,6}))),,-LOOKUP(1,-RIGHT(TRIM(LEFT(D1,FIND("g/L",D1)-1)),{1,2,3,4,5,6})))
 
Upvote 0
Or better yet:

=IF(ISERROR(FIND("g/L",D1)),,-LOOKUP(1,-RIGHT(TRIM(LEFT(D1,FIND("g/L",D1)-1)),{1,2,3,4,5,6})))
 
Upvote 0

Forum statistics

Threads
1,215,245
Messages
6,123,842
Members
449,129
Latest member
krishnamadison

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