Extract numbers from strings in two different collumns if a certain set of letters follows. Choice of column based on IF statement

augz123

New Member
Joined
Aug 25, 2015
Messages
8
So, I have two columns where both might possibly contain the numbers I want to extract to a third column in the same row. The numbers I am looking to extract will allways be followed by a "G", an "L" or a "KG". As G denotes grams it would be great if the numbers extracted before a G would come out with the comma moved three spaces forward, i.e. so that 24G would become 0,0024 in the third column.

In the first column, A, there will only be the unit of measurement preceded by the number, but in many cases column A will be empty (see table for data). In those cases I'd like to check the longer text/number string in column B, same row, for the numbers. In column B the number might be preceeded by another number and an X like this: 8X150G. In those cases I would like only the 150 to be extracted.

The commas are not a problem as my country for some reason has choses a different standard than the English one.

Having tried on my own to no avail I realise that this requires quite a bit of work so all help is much appreciated, even only for the first column. A formula would be preferable but I am of course open to VBA as well.

I am using Excel 2013 on Windows 8.


AB
2,5KG

<tbody>
</tbody>
STORFE HØYRYGG STRIMLET GILDE

<tbody>
</tbody>
0,7L

<tbody>
</tbody>
BLÅBÆRSAFT 0,7L LERUM

<tbody>
</tbody>
3 KG JARLSBERG 27% KUVERT

<tbody>
</tbody>
419863 SALAMI SKIVET 8X150G ENH

<tbody>
</tbody>
431087 LAM LAPSKAUSKJØTT SALT TERNET FRYST

<tbody>
</tbody>
150G

<tbody>
</tbody>
NORVEGIA 27% SKIVET 150G TINE

<tbody>
</tbody>
468351 SVINEBOG SALT KOKT TERNET 5KG

<tbody>
</tbody>
24GBRINGEBÆRSYLTETØY KUVERT 24G LERUM
200STK

<tbody>
</tbody>
SMØR KUVERT 12G

<tbody>
</tbody>
5KG

<tbody>
</tbody>
RØKT KJØTTPØLSE UTEN SKINN 2X2,5KG

<tbody>
</tbody>
0,9L

<tbody>
</tbody>
HUSHOLDNINGSSAFT U/SUKKER 0,9L LERUM

<tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
May be,

Change your formula

=IF(ISBLANK(l4332);B2,IFERROR(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"L",""),"KG",""),"G","")/1000,A2))

into this :

=IF(ISBLANK(l4332);B2,IFERROR(SUBSTITUTE(A2,"G","")/1000,SUBSTITUTE(SUBSTITUTE(A2,"L",""),"KG","")))


Regards

This works, thanks!
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Here's the first part

Code:
Sub ams()
    Dim lr As Long, i As Long
    lr = Range("B" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    For i = 1 To lr
    If Range("A" & i) Like "*KG" Then
    Range("C" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 2) * 0.0001
    ElseIf Range("A" & i) Like "*G" Then
    Range("C" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 1)
    ElseIf Range("A" & i) Like "*L" Then
    Range("C" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 1)
    End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub

This works like a charm, thanks. I had to move the "* 0.0001" part down two rows and take away a zero, but otherwise perfect
 
Upvote 0
Here is the version which does not have the units. Following the method I suggested you should get a result similar to mine (see below).

Code:
Function FindUnit(s As String) As String
    With CreateObject("VBScript.Regexp")
        .ignorecase = True
        .Global = True
        .Pattern = "([\d,]+[ ]*(kg|g|l))[ ]|.+?"
        If .test(s) Then FindUnit = .Replace(s, "$1")
        p = InStr(1, FindUnit, "KG")
        If p > 0 Then FindUnit = Mid(FindUnit, 1, p - 1)
        x = InStr(1, FindUnit, "G")
       If x > 0 Then FindUnit = (Mid(FindUnit, 1, Len(FindUnit) - 1) / 1000)
        Z = InStr(1, FindUnit, "L")
        If Z > 0 Then FindUnit = Mid(FindUnit, 1, Z - 1)
    End With
End Function
Excel Workbook
ABCD
12,5KGSTORFE HOYRYGG STRIMLET GILDE2,5KG STORFE HOYRYGG STRIMLET GILDE2,5
2
30,7LBLABARSAFT 0,7l LERUM0,7L BLABARSAFT 0,7l LERUM0,7
4
53 KG JARLSBERG 27% KUVERT3 KG JARLSBERG 27% KUVERT3
6
7419863 SALAMI SKIVET 8X150G ENH419863 SALAMI SKIVET 8X150G ENH0,15
8
9431087 LAM LAPSKAUSKJOTT SALT TERNET FRYST431087 LAM LAPSKAUSKJOTT SALT TERNET FRYST
10
11150GNORVEGIA 27% SKIVET 150G TINE150G NORVEGIA 27% SKIVET TINE0,15
12
13468351 SVINEBOG SALT KOKT TERNET 5KG468351 SVINEBOG SALT KOKT TERNET 5KG5
14
1524GBRINGEBARSYLTETOY KUVERT 24G LERUM24G BRINGEBARSYLTETOY KUVERT LERUM0,024
16200STKSMOR KUVERT 12G200STK SMOR KUVERT 12G0,012
17
185KGROKT KJOTTPOLSE UTEN SKINN 2X2,6KG5KG ROKT KJOTTPOLSE UTEN SKINN 2X2,6KG5
19
200,9LHUSHOLDNINGSSAFT U/SUKKER 0,9L LERUM0,9L HUSHOLDNINGSSAFT U/SUKKER LERUM0,9
Sheet
 
Upvote 0
Here is the version which does not have the units. Following the method I suggested you should get a result similar to mine (see below).

Code:
Function FindUnit(s As String) As String
    With CreateObject("VBScript.Regexp")
        .ignorecase = True
        .Global = True
        .Pattern = "([\d,]+[ ]*(kg|g|l))[ ]|.+?"
        If .test(s) Then FindUnit = .Replace(s, "$1")
        p = InStr(1, FindUnit, "KG")
        If p > 0 Then FindUnit = Mid(FindUnit, 1, p - 1)
        x = InStr(1, FindUnit, "G")
       If x > 0 Then FindUnit = (Mid(FindUnit, 1, Len(FindUnit) - 1) / 1000)
        Z = InStr(1, FindUnit, "L")
        If Z > 0 Then FindUnit = Mid(FindUnit, 1, Z - 1)
    End With
End Function

ABCD
12,5KGSTORFE HOYRYGG STRIMLET GILDE2,5KG STORFE HOYRYGG STRIMLET GILDE2,5
2
30,7LBLABARSAFT 0,7l LERUM0,7L BLABARSAFT 0,7l LERUM0,7
4
53 KG JARLSBERG 27% KUVERT 3 KG JARLSBERG 27% KUVERT3
6
7419863 SALAMI SKIVET 8X150G ENH 419863 SALAMI SKIVET 8X150G ENH0,15
8
9431087 LAM LAPSKAUSKJOTT SALT TERNET FRYST 431087 LAM LAPSKAUSKJOTT SALT TERNET FRYST
10
11150GNORVEGIA 27% SKIVET 150G TINE150G NORVEGIA 27% SKIVET TINE0,15
12
13468351 SVINEBOG SALT KOKT TERNET 5KG 468351 SVINEBOG SALT KOKT TERNET 5KG5
14
1524GBRINGEBARSYLTETOY KUVERT 24G LERUM24G BRINGEBARSYLTETOY KUVERT LERUM0,024
16200STKSMOR KUVERT 12G200STK SMOR KUVERT 12G0,012
17
185KGROKT KJOTTPOLSE UTEN SKINN 2X2,6KG5KG ROKT KJOTTPOLSE UTEN SKINN 2X2,6KG5
19
200,9LHUSHOLDNINGSSAFT U/SUKKER 0,9L LERUM0,9L HUSHOLDNINGSSAFT U/SUKKER LERUM0,9

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

This worked perfectly, thanks!

I see that I need to extract the the numbers before the X's (the standalone 2 below) as well into a separate colum, but I think I'll be able to do it with the what you and the others in the thread have provided.

ROKT KJOTTPOLSE UTEN SKINN 2X2,6KG
 
Upvote 0
In this line:

ROKT KJOTTPOLSE UTEN SKINN 2X2,6KG

I changed your original
2,5 to 2,6 just to test something at an earlier version of my code.


 
Upvote 0
You can do this with a formula for column A, rather than VBA.

I would add two columns to store the data.

Formula for quantity: =IF(IFERROR(LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("KG",A2))+1)),"N/A")="N/A",IF(IFERROR(LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("L",A2))+1)),"N/A")="N/A",IF(IFERROR(LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("G",A2))+1)),"N/A")="N/A","Missing",LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("G",A2))+1))),LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("L",A2))+1))),LEFT(A2,LEN(A2)-(LEN(A2)-(FIND("KG",A2))+1)))

Formula for unit of measure: =IF(IFERROR(MID(A2,FIND("KG",A2),LEN(A2)-(LEN(A2)-(FIND("KG",A2))+1)),"N/A")="N/A",IF(IFERROR(MID(A2,FIND("L",A2),LEN(A2)-(LEN(A2)-(FIND("L",A2))+1)),"N/A")="N/A",IF(IFERROR(MID(A2,FIND("G",A2),LEN(A2)-(LEN(A2)-(FIND("G",A2))+1)),"N/A")="N/A","Missing",MID(A2,FIND("G",A2),LEN(A2)-(LEN(A2)-(FIND("G",A2))+1))),MID(A2,FIND("L",A2),LEN(A2)-(LEN(A2)-(FIND("L",A2))+1))),MID(A2,FIND("KG",A2),LEN(A2)-(LEN(A2)-(FIND("KG",A2))+1)))

Using the formulas will work better if you have other people using the file and don't want them having VBA issues, or if internal security prevents enabling VBA.

I'm not certain how to parse the column B text string using this formula though. That might require the VBA scripting option.
 
Upvote 0
If you need a formula, try this. Enter it into C1 and copy down:

=IFERROR(-LOOKUP(1;-MID(A1&" "&B1&" ";FIND("KG ";A1&" "&B1&" ")-{1;2;3;4;5;6;7;8};{1;2;3;4;5;6;7;8}));"")&IFERROR(-LOOKUP(1;-MID(A1&" "&B1&" ";FIND("L ";A1&" "&B1&" "&"L ")-{1;2;3;4;5;6;7;8};{1;2;3;4;5;6;7;8}));"")&IFERROR(-LOOKUP(1;-MID(A1&" "&B1&" ";FIND("G ";A1&" "&B1&" "&"G ")-{1;2;3;4;5;6;7;8};{1;2;3;4;5;6;7;8}))/1000;"")
Excel Workbook
ABC
12,5KGSTORFE HOYRYGG STRIMLET GILDE2,5
2
30,7LBLABARSAFT 0,7l LERUM0,7
4sdfsdf ddfde 4KG jaheree4
53 KG JARLSBERG 27% KUVERT3
6
7419863 SALAMI SKIVET 8X150G ENH0,15
8
9431087 LAM LAPSKAUSKJOTT SALT TERNET FRYST
10
11150GNORVEGIA 27% SKIVET 150G TINE0,15
12
13468351 SVINEBOG SALT KOKT TERNET 5KG5
14
1524GBRINGEBARSYLTETOY KUVERT 24G LERUM0,024
16200STKSMOR KUVERT 12G0,012
17
185KGROKT KJOTTPOLSE UTEN SKINN 2X2,5KG5
19
200,9LHUSHOLDNINGSSAFT U/SUKKER 0,9L LERUM0,9
Sheet
 
Upvote 0
Note: the formula in post # will give false negative if the data to extract is preceded by a word ending in the unit, for example:


JARLSBERG 27% KUVERT 150G


It won't extract 150.
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

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