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
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi Peter,
a) Could there really be blank cells in the column D criteria list or did that only happen because you decided later to insert a blank row in your sample data?
No Blank in Criteria.
b )if future change in Criteria list it should accomodate
Criteria Account Code
7350 :=> Ignore 7350 Keep 735
600
165

3) Criteria will be Inner loop
Means My Criteria and Invoice will not be be same row.
LR_Invoices = Range("A"&rows.count).end(xlup).row
Criteria_LastRow = Range("A"&rows.count).end(xlup).row

for i = 2 to LR_Invoices
for j = 2 to Criteria_LastRow
if cells(i,j) = account code
cells(i,2).value = account no ' Update Column 2
exit j
end if
next j
next i
 
Upvote 0
Sorry, more questions I'm afraid

Full Account No is Column D. Account No should begin with specified digit.
What if there is more than one?

eg In post 17 sample data there are cells that have multiple matches for account number starts ..
A3: P25125/6000000453 RW
A6: D-W-10983-3150004927
A9: refu25188/6000000502

In each case you have listed the first of these in your expected results. Just checking: Is that the requirement or you don't care which one is returned, or both should be returned?
 
Upvote 0
Hi Peter,

It will be first match, there will not be duplicate account code in actual data. Thanks
it was my typing mistake.

Thanks.
Mg
 
Last edited:
Upvote 0
there will not be duplicate account code in actual data.
In that case try

VBA Code:
Sub Get_Account_Number()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  RX.Pattern = "(^|\D)(" & Join(Application.Transpose(Range("D2", Range("D" & Rows.Count).End(xlUp))), "\d*|") & "\d*)(?=\D|$)"
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If RX.Test(a(i, 1)) Then b(i, 1) = RX.Execute(a(i, 1))(0).submatches(1)
  Next i
  Range("B2").Resize(UBound(a)).Value = b
End Sub
 
Upvote 0
Hi Peter,

It worked as expected, Millions of thanks for your help. It will Save my lot of time,
Daily we use to check manually 10000 records. (y) ??



Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
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