VBA - Split Data into 4 columns

Mallesh23

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

Need your help in splitting Data, Expecting 90% accuracy... if not possible 100% as data is not structured.
Column A is Consolidated Invoices of all Company's. hence data is unstructured.

Logic as follows:-
Split the Data of Column A, with Charecter "/","-","Blank",

if there is a Join of Number and Text split that also separating number and text.

After Splitting Column A

If there are two Numbers - Big Length of Number is Invoice no - Put it in Column B and
Small Length of Number put into Column C. as product Code,


also if there are two text after split , Big text in Column D as Abbreviation
Small text is in Column E as Abbreviation.

If 3 text after split, put 3 split in column F.

Below is a Table with Expected output is in Column B,C,D


Book3
ABCDEF
1DataInvoice No (Big No)Product Code Product AbbreviationProduct Abbreviation-2Misc
25300000798-PIF-PR5300000798-PIFPR
37350001120 RW BAL7350001120-BALRW
45100000182-Bal-20510000018220BAL-
5D-PR-10967-31500049231500049210967PRD
6F-W-10713-3150004797315000479710713FW
72333-RW-4400000176-P44000001762333RWP
862001105926200110592---
97800000292-8451-P78000002928451P-
10635011186 - 10 - p63501118610P-
11D-ED-10566-31500047031500047010566EDD
12F-W-10488-3150004700315000470010488FW
135650000669/W&D/DEP5650000669-DEPW&D
145650000671/W&D/DEP5650000671-DEPW&D
155650000606 fnl pmt S5650000606-FNLPMTS
165750000669-bal-35575000066935BAL-
17refu25188/6000000502600000050225188Refu-
189161/4750000388/vs/p47500003889161vsp
19F-ED-9861-315000440431500044049861EDf
206550000757fp10655000075710FP-
219202/4750000419/VS/P47500004199202VSP
227250000387RWDEP7250000387-RWDEP-
239619/4750000712/RW/C47500007129619RWC
24F-W-10455-3150004682315000468210455FW
25D-W-11009-3150004941315000494111009DW
2661001540222366-10-P6.10015E+1310p-
2762000007806200000780---
28F-RW-10212-0004571457110212RWF
29P25132/6000000466 RW6000000466-P25132RW
302365-ED-4400000196-D44000001962365EDD
3124903 RW/PP (ML)24903- RWPP (ML)
326550000363dp10655000036310DP-
335.49269E+154.92693E+13---
346350000532 - 10 - D635000053210D-
356550000882dp30655000088230dp-
36D/P/5050000791-16762505000079116762DP
37f-sr-3150003955-885331500039558853SRF
386150000163/Refunded Dep/Cancelled Contract/10615000016310Refunded DepCancelled Contract
39AX4150000361841500003618-AX-
404150000330 - D4150000330-D-
4124318 PR/PO24318-PRPO
424300001631 - 10 - P430000163110P-
43DP-PR-#2394-2812394281DPPR
Sheet1




Rich (BB code):
Below solution extract Invoices in Column B...works successfually. Got solution from this forum only. Need Extra output in C,D,E,F Columns/CODE]


	
	
	
	
	
	


VBA Code:
Sub Extract()

    Dim sh As Worksheet
    Set sh = ThisWorkbook.Worksheets("Sheet1")
   
    Dim str_Out As String
    Dim s As String
   
   
    lr = sh.Range("A" & Rows.Count).End(xlUp).Row
   
       
    For i = 2 To lr
           
        s = sh.Cells(i, 1)
     
        sh.Cells(i, 2).Value = ExtractInvoices(s)
   
    Next i


    MsgBox "Macro successful"

End Sub



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
Thanks mg
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
In G2, put =TRIM(SUBSTITUTE(SUBSTITUTE(A2,"-"," "),"/"," ")) to turn all the delimiters into spaces
In H2, =REPT(" ",256)&SUBSTITUTE(G2," ",REPT(" ",255)) to spread them all out
In J2 =TRIM(MID($H2,COLUMN(A1)*255,255)) to return the first delimited item. Drag right to M2 for the other items.
In O2 and dragged right to R2,=IF(ISNUMBER(J2+0),J2+0,J2) to turn the numerals in J2:M2 into numbers
Then the results columns
In T2, =IF(COUNT(O2:R2)>0,MAX(O2:R2),"-")
In U2, =IF(COUNT(O2:R2)>1,MIN(O2:R2),"-")
In V2, =IF(ISNUMBER(O2),IF(ISNUMBER(P2),Q2,P2),O2)
And in W2 =TRIM((SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT($G2,LEN($A2)+2),T2," ",1),U2," ",1),V2," ")))

You will still have to deal with some situations by hand, like row 38 where the spaces are data and not delimiters.
 
Upvote 0
Hi Mikerickson,

Works great, millions of thank for your effort , it worked as expected. ?



Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,215,601
Messages
6,125,758
Members
449,259
Latest member
rehanahmadawan

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