VBA - Split the data

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Column A is a Consolidated Invoices of different Country's Company. different style of drafting.

Now task is extract Invoices separately. Expected Output is in Column B.

I am Unable to split the data as Column A data is not a standard one.

Looking for a Macro. Thanks.

Sample dummy data

Book1
AB
1InvoiceExpected Invoice
25300000798-PIF-PR5300000798
37350001120 RW BAL7350001120
45100000182-Bal-205100000182
5D-PR-10967-315000492315000492
6F-W-10713-3150004797
72333-RW-4400000176-P4400000176
862001105926200110592
97800000292-8451-P7800000292
10635011186 - 10 - p635011186
11D-ED-10566-315000470315000470
12F-W-10488-31500047003150004700
135650000669/W&D/DEP5650000669
145650000671/W&D/DEP5650000671
155650000606 fnl pmt S5650000606
165750000669-bal-355750000669
17refu25188/60000005026000000502
189161/4750000388/vs/p4750000388
19F-ED-9861-31500044043150004404
206550000757fp106550000757
219202/4750000419/VS/P4750000419
227250000387RWDEP7250000387
239619/4750000712/RW/C4750000712
24F-W-10455-31500046823150004682
25D-W-11009-31500049413150004941
2661001540222366-10-P6.10015E+13
2762000007806200000780
28F-RW-10212-0004571
29P25132/6000000466 RW6000000466
302365-ED-4400000196-D4400000196
3124903 RW/PP (ML)
326550000363dp106550000363
3354926931649657885492693164965788
346350000532 - 10 - D6350000532
356550000882dp306550000882
36D/P/5050000791-167625050000791
37f-sr-3150003955-88533150003955
386150000163/Refunded Dep/Cancelled Contract/106150000163
39AX4150000361841500003618
404150000330 - D
4124318 PR/PO
424300001631 - 10 - P4300001631
43DP-PR-#2394-281
Sheet1


Thanks
mg
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I can't see the logic as to why some rows get a value in the Expected Invoice column and some do not. You have given no explanation about how we would decide what is an invoice number and what is not.
 
Upvote 0
Hi Peter,

after splitting a cell , Invoice should be in no, if multiple numbers after split then Number which has highest Len(Numbers) will be accurate invoice
if cell value Blank - No Output for that cell.


Thanks.
mg
 
Upvote 0
Try this:
Code:
Public Function ExtractInvoices(ByVal s As String) As String
Dim i As Long, k As Long, v As Variant
For i = 1 To Len(s)
    If IsNumeric(Mid(s, i, 1)) = False Then Mid(s, i, 1) = " "
Next
For Each v In Split(Application.Trim(s), " ")
    If Len(v) > k Then
        k = Len(v)
        ExtractInvoices = v
    End If
Next

End Function
 
Upvote 0
Hi Phuoc

Thanks for your help, My data is 50000 records. but still it worked. it is giving correct output as asked.


Thanks
mg
 
Upvote 0
it worked.
OK, that indicates that the blank cells in column B of post 1 should not have been blank - that was one of the things I was asking about before. ;)

If you want the results to be dynamic (that is a function), this is a little faster but not by a huge amount. For me with 50,000 rows about 1.2 seconds for this function v about 2.0 seconds for Phuoc's function.

VBA Code:
Function InvoiceNum(s As String) As String
  Static RX As Object
  Dim itm As Variant
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.Pattern = "\d+"
  End If
  For Each itm In RX.Execute(s)
    If Len(itm) > Len(InvoiceNum) Then InvoiceNum = itm
  Next itm
End Function

If you don't need the results to be dynamic, then this is much faster (0.25 seconds for me on the same data)

VBA Code:
Sub Get_Invoices()
  Dim RX As Object
  Dim a As Variant, itm As Variant
  Dim tmp As String
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "\d+"
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    tmp = vbNullString
    For Each itm In RX.Execute(a(i, 1))
      If Len(itm) > Len(tmp) Then tmp = itm
    Next itm
    a(i, 1) = tmp
  Next i
  With Range("B2").Resize(UBound(a))
    .NumberFormat = "@"
    .Value = a
  End With
End Sub
 
Upvote 0
Hi Peter,

Wow !! this is amazing piece of code, tested and it worked. ?(y)

Can you please add a comment to know how it works.

Thanks
mg
 
Upvote 0
Can you please add a comment to know how it works.
If you are not familiar with 'Regular Expressions' (VBScript.RegExp) then you might need to do some research on that. The RegExp looks for patterns in a string. The pattern I set, "\d+", simply looks for 1 or more digits in a row. The code then looks through any such matches and gets the longest one.
 
Upvote 0
If the number being retrieved will not be longer than 15 digits, this function should also work...
VBA Code:
Function ExtractInvoices(ByVal S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  ExtractInvoices = Evaluate("MAX(" & Replace(Application.Trim(S), " ", ",") & ")")
End Function
 
Upvote 0
this function should also work...
The OP stated that the previous functions/code worked. I think that your functions fails each of these points
Number which has highest Len(Numbers) will be accurate invoice
See row 28 of the sample data

if cell value Blank - No Output for that cell.


A similar function to yours that I believe would work is as below, though I think the problem with a udf for this with 50,000 records is a calculation speed one, hence my non-udf suggestion above.
VBA Code:
Function ExtractInvoice(ByVal S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = ","
  Next
  ExtractInvoice = Mid(Evaluate("MAX(1," & Replace(S, ",", ",1") & ")"), 2)
End Function
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,947
Members
449,480
Latest member
yesitisasport

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