split column a data with delimiter

Mallesh23

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

I have data in column A, and I want to split column a with delimiter _
Data is my Column and I am expecting result as below by using split function in vba. thanks in advance for your help.

Dataoutput
abc_pqr_xyzabcpqrxyz
pqr_abc_xyzpqrabcxyz
xyz_pqr_xyzxyzpqrxyz
abc_pqr_xyzabcpqrxyz
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why do you need VBA? use Text to Columns. If you do insist on VBA record yourself doing it.
 
Upvote 0
If you don't use Text To Columns, here's one approach:

Book1
ABCD
1Data
2abc_pqr_xyzabcpqrxyz
3pqr_abc_xyzpqrabcxyz
4xyz_pqr_xyzxyzpqrxyz
5abc_pqr_xyzabcpqrxyz
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=LEFT(A2,FIND("_",A2)-1)
C2:C5C2=MID(A2,FIND("@",SUBSTITUTE(A2,"_","@",1))+1,(FIND("@",SUBSTITUTE(A2,"_","@",2))-1)-(FIND("@",SUBSTITUTE(A2,"_","@",1))-1)-1)
D2:D5D2=RIGHT(A2,LEN(A2)-FIND("@",SUBSTITUTE(A2,"_","@",2)))
 
Upvote 0
Hi Mark and Kweaver,
Thanks for your response, its working, but actually I was trying to split it through vba split function.
and looping through all cells for i = 0 to ubound(arr) like this kind.


Regards
mg
 
Upvote 0
Try:
VBA Code:
Sub a1117811a()
Dim c As Range, ary
Application.ScreenUpdating = False
For Each c In Range("A2", Cells(Rows.count, "A").End(xlUp))
    ary = Split(c, "_")
    c.Offset(, 1).Resize(1, UBound(ary) + 1) = ary
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
@Mallesh23,I still don't see the advantage in looping into an array to put it in the cells in one go when text to columns does it already but Akuini has posted the code to give you the option you asked for
 
Upvote 0
Just a couple of timings...
2240 records... best time of 5 runs with the 2 codes below... timed by FastExcel V3.
Code a1117811a = 560.38 milliseconds
Code TtoCol = 23.69 milliseconds

Code:
Sub a1117811a()
Dim c As Range, ary
Application.ScreenUpdating = False
For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    ary = Split(c, "_")
    c.Offset(, 1).Resize(1, UBound(ary) + 1) = ary
Next
Application.ScreenUpdating = True
End Sub

Code:
Sub TtoCol()
Columns(1).TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
        TrailingMinusNumbers:=True
End Sub
 
Upvote 0
Hi Mark and Akuini,

Thanks you so much both of you for giving me both the option,
Actually I wanted to learn the method of vba spliting a text.


thanks
mg
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,955
Members
449,199
Latest member
Riley Johnson

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