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!!!
 
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
i run have error as below picture
1742712506015.png

Thanks you!!!
 
Upvote 0
Try.
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)
If A(T, 2) <> "" And A(T, 3) <> "" Then
X = Split(A(T, 2), "|")
Y = Split(A(T, 3), "|")

    For Ta = 0 To WorksheetFunction.Min(UBound(X, 1), UBound(Y, 1))
    Z = Z + 1
    If X(Ta) <> "" And Y(Ta) <> "" Then
    Dic.Item(Z) = Array(A(T, 1), Trim(X(Ta)), Trim(Y(Ta)))
    End If
    Next Ta
X = "": Y = ""
End If
Next T

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

End Sub
 
Upvote 0
May be another approach,
VBA Code:
Sub test()
    Dim X%, Y%, Z%, xA$, xB, xC
    Dim xD As Worksheet
    Dim xR&

    Set xD = ActiveSheet
    xR = 1
    Z = xD.Cells(xD.Rows.Count, "A").End(xlUp).Row

    For X = 1 To Z
        xA = xD.Cells(X, "A").Value
        xB = Split(xD.Cells(X, "B").Value, "|")
        xC = Split(xD.Cells(X, "C").Value, "|")

        For Y = 0 To Application.WorksheetFunction.Max(UBound(xB), UBound(xC))
            xD.Cells(xR, "E").Value = xA
            If Y <= UBound(xB) Then xD.Cells(xR, "F").Value = Trim(xB(Y))
            If Y <= UBound(xC) Then xD.Cells(xR, "G").Value = Trim(xC(Y))
            xR = xR + 1
        Next Y
    Next X
End Sub
 
Upvote 0
Sub test() Dim X%, Y%, Z%, xA$, xB, xC Dim xD As Worksheet Dim xR& Set xD = ActiveSheet xR = 1 Z = xD.Cells(xD.Rows.Count, "A").End(xlUp).Row For X = 1 To Z xA = xD.Cells(X, "A").Value xB = Split(xD.Cells(X, "B").Value, "|") xC = Split(xD.Cells(X, "C").Value, "|") For Y = 0 To Application.WorksheetFunction.Max(UBound(xB), UBound(xC)) xD.Cells(xR, "E").Value = xA If Y <= UBound(xB) Then xD.Cells(xR, "F").Value = Trim(xB(Y)) If Y <= UBound(xC) Then xD.Cells(xR, "G").Value = Trim(xC(Y)) xR = xR + 1 Next Y Next X End Sub
Thanks you so much!!!
 
Upvote 0
Try.
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)
If A(T, 2) <> "" And A(T, 3) <> "" Then
X = Split(A(T, 2), "|")
Y = Split(A(T, 3), "|")

    For Ta = 0 To WorksheetFunction.Min(UBound(X, 1), UBound(Y, 1))
    Z = Z + 1
    If X(Ta) <> "" And Y(Ta) <> "" Then
    Dic.Item(Z) = Array(A(T, 1), Trim(X(Ta)), Trim(Y(Ta)))
    End If
    Next Ta
X = "": Y = ""
End If
Next T

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

End Sub
Great, thanks you so much!!!
 
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