Help me code VBA split string!!!

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
188
Office Version
  1. 2016
Platform
  1. Windows
i have column A,B, C as below
1742632520034.png

I want split column B,C with character "|" result as picture below
1742632585230.png

Thanks and Best regards!!!
 
Each cell in column B and C has a character "|", and only one "|" ?
 
Upvote 0
Input data
Book1.xlsm
ABC
1100aaa | ddd126565|9877
2101aaa | ddd226565|9878
3102aaa | ddd326565|9879
4
Sheet1

Output data
Book1.xlsm
ABC
1100aaa26565
2100ddd19877
3101aaa26565
4101ddd29878
5102aaa26565
6102ddd39879
7
Sheet2


VBA code.
VBA Code:
Sub Splitdata()
Dim A, X, Y
Dim Dic As Object
Dim Lr&, T&, Ta&, Z&
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Sheet1")
Lr = .Range("A" & Rows.Count).End(xlUp).Row
A = .Range("A1:C" & Lr)
End With
For T = 1 To UBound(A, 1)
X = Split(A(T, 2), "|")
Y = Split(A(T, 3), "|")
    For Ta = 0 To UBound(X, 1)
    Z = Z + 1
    Dic.Item(Z) = Array(A(T, 1), Trim(X(Ta)), Trim(Y(Ta)))
    Next Ta
X = "": Y = ""
Next T

With Sheets("Sheet2").Range("A1")
.CurrentRegion.Clear
.Resize(Z, 3) = Application.Index(Dic.items, 0, 0)
End With

End Sub
 
Upvote 0
Try.
VBA Code:
Sub mySplit()
    Dim Rng As Range
    Set Rng = Range("A1") 'A1 is the first cell of data
    
    Dim Output As Range
    Set Output = Range("E1") 'E1 is the first cell of output cells
    
    For i = 0 To Rng.CurrentRegion.Rows.Count - 1
        For j = 0 To 1
            Output.Offset(i * 2 + j, 0) = Rng.Offset(i, 0)
            Output.Offset(i * 2 + j, 1) = Split(Rng.Offset(i, 1), "|")(j)
            Output.Offset(i * 2 + j, 2) = Split(Rng.Offset(i, 2), "|")(j)
        Next j
    Next i
End Sub
 
Upvote 0
Input data
Book1.xlsm
ABC
1100aaa | ddd126565|9877
2101aaa | ddd226565|9878
3102aaa | ddd326565|9879
4
Sheet1

Output data
Book1.xlsm
ABC
1100aaa26565
2100ddd19877
3101aaa26565
4101ddd29878
5102aaa26565
6102ddd39879
7
Sheet2


VBA code.
VBA Code:
Sub Splitdata()
Dim A, X, Y
Dim Dic As Object
Dim Lr&, T&, Ta&, Z&
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Sheet1")
Lr = .Range("A" & Rows.Count).End(xlUp).Row
A = .Range("A1:C" & Lr)
End With
For T = 1 To UBound(A, 1)
X = Split(A(T, 2), "|")
Y = Split(A(T, 3), "|")
    For Ta = 0 To UBound(X, 1)
    Z = Z + 1
    Dic.Item(Z) = Array(A(T, 1), Trim(X(Ta)), Trim(Y(Ta)))
    Next Ta
X = "": Y = ""
Next T

With Sheets("Sheet2").Range("A1")
.CurrentRegion.Clear
.Resize(Z, 3) = Application.Index(Dic.items, 0, 0)
End With

End Sub
Thanks you so much!!!
 
Upvote 0
Try.
VBA Code:
Sub mySplit()
    Dim Rng As Range
    Set Rng = Range("A1") 'A1 is the first cell of data
   
    Dim Output As Range
    Set Output = Range("E1") 'E1 is the first cell of output cells
   
    For i = 0 To Rng.CurrentRegion.Rows.Count - 1
        For j = 0 To 1
            Output.Offset(i * 2 + j, 0) = Rng.Offset(i, 0)
            Output.Offset(i * 2 + j, 1) = Split(Rng.Offset(i, 1), "|")(j)
            Output.Offset(i * 2 + j, 2) = Split(Rng.Offset(i, 2), "|")(j)
        Next j
    Next i
End Sub
Thanks you so much!!!
 
Upvote 0
Each cell in column B and C has a character "|", and only one "|" ?
If not always do have, try this one.
VBA Code:
Sub mySplit()
    Dim Rng As Range
    Set Rng = Range("A1") 'A1 is the first cell of data
    
    Dim Output As Range
    Set Output = Range("E1") 'E1 is the first cell of output cells
    
    For i = 0 To Rng.CurrentRegion.Rows.Count - 1
        mySplit_1 = Split(Rng.Offset(i, 1), "|")
        mySplit_2 = Split(Rng.Offset(i, 2), "|")
        If UBound(mySplit_1) <= UBound(mySplit_2) Then
            myUbound = UBound(mySplit_1)
        Else
            myUbound = UBound(mySplit_2)
        End If
        For j = 0 To myUbound
            Output.Offset(k, 0) = Rng.Offset(i, 0)
            Output.Offset(k, 1) = mySplit_1(j)
            Output.Offset(k, 2) = mySplit_2(j)
            k = k + 1
        Next j
    Next i
End Sub
 
Upvote 0
Each cell in column B and C has a character "|", and only one "|" ?
If not always do have, try this one.
VBA Code:
Sub mySplit()
    Dim Rng As Range
    Set Rng = Range("A1") 'A1 is the first cell of data
   
    Dim Output As Range
    Set Output = Range("E1") 'E1 is the first cell of output cells
   
    For i = 0 To Rng.CurrentRegion.Rows.Count - 1
        mySplit_1 = Split(Rng.Offset(i, 1), "|")
        mySplit_2 = Split(Rng.Offset(i, 2), "|")
        If UBound(mySplit_1) <= UBound(mySplit_2) Then
            myUbound = UBound(mySplit_1)
        Else
            myUbound = UBound(mySplit_2)
        End If
        For j = 0 To myUbound
            Output.Offset(k, 0) = Rng.Offset(i, 0)
            Output.Offset(k, 1) = mySplit_1(j)
            Output.Offset(k, 2) = mySplit_2(j)
            k = k + 1
        Next j
    Next i
End Sub
Thanks you so much!!!
Best regards,
Nguyen Anh Dung
 
Upvote 0

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