How to extract multiple numbers from one cell, to multiple rows?

user47

New Member
Joined
Apr 6, 2013
Messages
17
Office Version
  1. 365
Good day,

I have a dataset with a column of thousands of rows of invoice numbers.
Sometimes the invoice numbers appears once per row, sometimes multiple invoice numbers appear in one row (always in the same column).
I can only manage to extract the first instance of the invoice number in all cases.
Is there any way to do for the cases where they appear multiple times (beside manually)?

Thank you.


Sample Data.xlsx
A
2(data is messy like this this)
3Invoices
4Inv. 1234, Inv. 12345
5Inv. 4569
6Inv 6467, Inv. 3835, Inv. 4786
7Inv.5672, Inv. 3199, Inv.41165
8
9
10(need to get like this)
11Inv #
121234
1312345
144569
156467
163835
174786
185672
193199
2041165
Sheet2
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to Mr. Excel. Please update your profile to indicate which version of Excel you run.
 
Upvote 0
Hi

I would go for PQ.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Invoices"]}[Content],
    #"Replaced Value" = Table.ReplaceValue(Source,"Inv","",Replacer.ReplaceText,{"Invoices"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",".","",Replacer.ReplaceText,{"Invoices"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1"," ","",Replacer.ReplaceText,{"Invoices"}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value2", {{"Invoices", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Invoices"),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Invoices", Int64.Type}})
in
    #"Changed Type"
 
Upvote 0
Solution
I played w/a macro with the results in the D column:

Code:
Sub SplitInv()
Dim str() As String, str1 As String, str2() As String, i As Long, j As Long, k As Long
Dim lr As Long, size As Integer, sz As Integer
size = 0
sz = 3
lr = Cells(Rows.Count, "A").End(xlUp).Row
For k = 3 To lr
str1 = Trim(Replace(Cells(k, "A"), "Inv. ", ""))
str2 = Split(str1, ",")
size = sz + UBound(str2)
j = 0
For i = sz To size
Cells(i, "D") = str2(j)
j = j + 1
Next i
sz = sz + 1 + UBound(str2)
Next k
End Sub
 
Upvote 0
You could also try this single formula in a single cell.

22 02 13.xlsm
A
1
2(data is messy like this this)
3Invoices
4Inv. 1234, Inv. 12345
5Inv. 4569
6Inv 6467, Inv. 3835, Inv. 4786
7Inv.5672, Inv. 3199, Inv.41165
8
9
10(need to get like this)
11Inv #
121234
1312345
144569
156467
163835
174786
185672
193199
2041165
21
Extract Numbers
Cell Formulas
RangeFormula
A12:A20A12=LET(all,CONCAT(A4:A7),one,MID(all,SEQUENCE(LEN(all)),1),FILTERXML("<p><c>"&SUBSTITUTE(TRIM(CONCAT(IF(ISNUMBER(one+0),one," ")))," ","</c><c>")&"</c></p>","//c"))
Dynamic array formulas.
 
Upvote 0
If a macro approach is preferred, here is another option, with results in column B

VBA Code:
Sub GetInvNumbers()
  Dim RX As Object
  Dim Nums As Variant
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "\D"
  Nums = Split(Application.Trim(RX.Replace(Join(Application.Transpose(Range("A4", Range("A4").End(xlDown)))), " ")))
  Range("B2").Resize(UBound(Nums) + 1).Value = Application.Transpose(Nums)
End Sub

22 02 13.xlsm
AB
1
2(data is messy like this this)1234
3Invoices12345
4Inv. 1234, Inv. 123454569
5Inv. 45696467
6Inv 6467, Inv. 3835, Inv. 47863835
7Inv.5672, Inv. 3199, Inv.411654786
85672
93199
1041165
11
Extract Numbers (vba)
 
Upvote 0
I have a dataset with a column of thousands of rows of invoice numbers.
I overlooked that in my responses. Depending on how many "thousands" are involved and how many invoices could appear in each row, either of my suggestions could possibly fail - or need modification.
 
Upvote 0
Thank you everyone so much for your responses.

I learnt a lot delving more into your solutions. I found user's "Shift-del" solution to be the simplest so far in power query using an index column as well.

Will mark this as solved.

Thanks again to all.
 
Upvote 0
Glad you got a Successful outcome. Thanks for letting us know. (y)
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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