extract multiple numbers from String

ravi2628

Board Regular
Joined
Dec 20, 2017
Messages
221
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi everyone Good Morning/Afternoon/Evening/Night,

i was looking for a logic to extract the numbers from a string,the data is as follows.

Input:
100IU/1ML10ML
100IU/1ML3ML
100IU/1ML3ML
100IU/1ML10ML
500Y/1ML1ML
1MG/1ML10ML

Output:
100IU/1ML10ML
100​
1​
10​
100IU/1ML3ML
100​
1​
3​
100IU/1ML3ML
100​
1​
3​
100IU/1ML10ML
100​
1​
10​
500Y/1ML1ML
500​
1​
1​
1MG/10ML10ML
1​
10​
10​

Output required columns are 3,4 is derived from Column 1
Output required column 5 is derived from column 2

Please help me out.

Regards,
Raviteja
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
If your texts always end in "ML", try this:

Dante Amor
ABCDE
1
2100IU/1ML10ML100110
3100IU/1ML3ML10013
4100IU/1ML3ML10013
5100IU/1ML10ML100110
6500Y/1ML1ML50011
71MG/1ML10ML1110
81000MG/10ML5ML1000105
Hoja15
Cell Formulas
RangeFormula
C2:C8C2=MAX(IFERROR(MID(LEFT(A2,FIND("/",A2)-1),COLUMN($A$1:$J$1),ROW($A$1:$A$10))+0,0))
D2:D8D2=TRIM(SUBSTITUTE(MID(A2,FIND("/",A2)+1,LEN(A2)),"ML",""))+0
E2:E8E2=SUBSTITUTE(B2,"ML","")+0
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
If your texts always end in "ML", try this:

Dante Amor
ABCDE
1
2100IU/1ML10ML100110
3100IU/1ML3ML10013
4100IU/1ML3ML10013
5100IU/1ML10ML100110
6500Y/1ML1ML50011
71MG/1ML10ML1110
81000MG/10ML5ML1000105
Hoja15
Cell Formulas
RangeFormula
C2:C8C2=MAX(IFERROR(MID(LEFT(A2,FIND("/",A2)-1),COLUMN($A$1:$J$1),ROW($A$1:$A$10))+0,0))
D2:D8D2=TRIM(SUBSTITUTE(MID(A2,FIND("/",A2)+1,LEN(A2)),"ML",""))+0
E2:E8E2=SUBSTITUTE(B2,"ML","")+0
Press CTRL+SHIFT+ENTER to enter array formulas.
Hi your logic is working but i have got an issue


Int-StrengthInt-Pack Vol
1.5MG/1ML0.5ML510.5


but the output need to be like this

Int-StrengthInt-Pack Vol
1.5MG/1ML0.5ML1.510.5


in column B i have remaining different Characters "ML"

0.4ML
500MG
1.1KG
1.2KG
2G
 
Upvote 0
Try these formulas in the indicated cells...

C2: =LOOKUP(9.9E+307,--LEFT(A2,ROW($1:$99)))

D2: =LOOKUP(9.9E+307,--LEFT(MID(A2,FIND("/",A2)+1,99),ROW($1:$99)))

E2: =LOOKUP(9.9E+307,--LEFT(B2,ROW($1:$99)))
 
Upvote 0
Try these formulas in the indicated cells...

C2: =LOOKUP(9.9E+307,--LEFT(A2,ROW($1:$99)))

D2: =LOOKUP(9.9E+307,--LEFT(MID(A2,FIND("/",A2)+1,99),ROW($1:$99)))

E2: =LOOKUP(9.9E+307,--LEFT(B2,ROW($1:$99)))
Hi Rick,

thanks for the Logic it works very well.

i need some help for this data also

Input:

8MG+100MG+2MG
900MG/1ML+60MG/1ML
97.5MG/5ML+100MG/5ML+9MG/5ML
7.5Y+750Y+162MG
67.5MG/5ML+6.8MG/5ML
67.5MG/5ML+6.8MG/10ML

Output:

8MG+100MG+2MG81002
900MG/1ML+60MG/1ML9001601
97.5MG/5ML+100MG/5ML+9MG/5ML97.55100595
7.5Y+750Y+162MG7.5750162
67.5MG/5ML+6.8MG/5ML67.556.85
67.5MG/5ML+6.8MG/10ML67.556.810

Can you please help me out
 
Upvote 0
8MG+100MG+2MG81002
900MG/1ML+60MG/1ML9001601
97.5MG/5ML+100MG/5ML+9MG/5ML97.55100595
7.5Y+750Y+162MG7.5750162
67.5MG/5ML+6.8MG/5ML67.556.85
67.5MG/5ML+6.8MG/10ML67.556.810

Try the following macro.
Assuming your data starts in A2, results in B2 onwards

VBA Code:
Sub extract_Numbers()
  Dim c As Range
  Dim i As Long, j As Long
  Dim b As Variant, m As Variant, d As Variant
  
  ReDim b(1 To Range("A" & Rows.Count).End(3).Row, 1 To 99)
  For Each c In Range("A2", Range("A" & Rows.Count).End(3))
    j = 0
    i = i + 1
    For Each m In Split(c, "+")
      For Each d In Split(m, "/")
        j = j + 1
        b(i, j) = Val(d)
      Next
      If UBound(Split(m, "/")) = 0 Then j = j + 1
    Next
  Next
  Range("B2").Resize(i, UBound(b, 2)).Value = b
End Sub
 
Upvote 0
This should do what you asked for in Message #5 spacing the numbers in the columns as you indicated you wanted...
VBA Code:
Sub BreakApartData()
  Dim R As Long, X As Long, Data As Variant, Arr As Variant
  Data = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value
  For R = 1 To UBound(Data)
    For X = 1 To Len(Data(R, 1))
      If Mid(Data(R, 1), X, 1) Like "[!0-9/+]" Then Mid(Data(R, 1), X) = " "
    Next
    Data(R, 1) = Replace(Data(R, 1), " ", "")
    Arr = Split(Data(R, 1), "+")
    For X = 0 To UBound(Arr)
      If Not Arr(X) Like "*/*" Then Arr(X) = Arr(X) & "/"
    Next
    Data(R, 1) = Join(Arr, "/")
  Next
  Application.ScreenUpdating = False
  With Range("B2").Resize(UBound(Data))
    .Value = Data
    .TextToColumns , xlDelimited, , , False, False, False, False, True, "/"
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try the following macro.
Assuming your data starts in A2, results in B2 onwards

VBA Code:
Sub extract_Numbers()
  Dim c As Range
  Dim i As Long, j As Long
  Dim b As Variant, m As Variant, d As Variant
 
  ReDim b(1 To Range("A" & Rows.Count).End(3).Row, 1 To 99)
  For Each c In Range("A2", Range("A" & Rows.Count).End(3))
    j = 0
    i = i + 1
    For Each m In Split(c, "+")
      For Each d In Split(m, "/")
        j = j + 1
        b(i, j) = Val(d)
      Next
      If UBound(Split(m, "/")) = 0 Then j = j + 1
    Next
  Next
  Range("B2").Resize(i, UBound(b, 2)).Value = b
End Sub
Thanks DanteAmor Your Logic Worked
 
Upvote 0
This should do what you asked for in Message #5 spacing the numbers in the columns as you indicated you wanted...
VBA Code:
Sub BreakApartData()
  Dim R As Long, X As Long, Data As Variant, Arr As Variant
  Data = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value
  For R = 1 To UBound(Data)
    For X = 1 To Len(Data(R, 1))
      If Mid(Data(R, 1), X, 1) Like "[!0-9/+]" Then Mid(Data(R, 1), X) = " "
    Next
    Data(R, 1) = Replace(Data(R, 1), " ", "")
    Arr = Split(Data(R, 1), "+")
    For X = 0 To UBound(Arr)
      If Not Arr(X) Like "*/*" Then Arr(X) = Arr(X) & "/"
    Next
    Data(R, 1) = Join(Arr, "/")
  Next
  Application.ScreenUpdating = False
  With Range("B2").Resize(UBound(Data))
    .Value = Data
    .TextToColumns , xlDelimited, , , False, False, False, False, True, "/"
  End With
  Application.ScreenUpdating = True
End Sub
Thanks Rick Your Logic Worked
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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