Sub TextToColumn(SourceRange As Range, Delimiter As String, ResultWs As Worksheet, ResultCell As String)
'SourceRange: vung du lieu can tach dong
'Delimiter: ky tu de phan tach dong
'ResultWs: Sheet dien ket qua
'ResultCell: o dien ket qua
Dim sArr(), Res(), Tmp
Dim Col As Byte, I As Integer, J As Byte, CountDelimiter As Byte, K As Integer
If SourceRange.Columns.Count > 1 Then Exit Sub
If Range(ResultCell).Rows.Count > 1 Or Range(ResultCell).Columns.Count > 1 Then Exit Sub
CountDelimiter = Len(CStr(SourceRange(1, 1))) - Len(Replace(CStr(SourceRange(1, 1)), Delimiter, "")) + 1
sArr() = SourceRange.Value
ReDim Res(1 To UBound(sArr, 1), 1 To CountDelimiter)
For I = 1 To UBound(sArr, 1)
Tmp = Split(sArr(I, 1), ",")
K = K + 1
For J = 0 To UBound(Tmp)
Res(K, J + 1) = Replace(CStr(Tmp(J)), """", "")
Next J
Next
ResultWs.Range(ResultCell).CurrentRegion.ClearContents
ResultWs.Range(ResultCell).Resize(K, CountDelimiter) = Res
ResultWs.Range(ResultCell).CurrentRegion.EntireColumn.AutoFit
End Sub
Sub Main()
Dim Rng As Range
Set Rng = Sheet1.Range("A1", Sheet1.Range("A1").End(xlDown))
Call TextToColumn(Rng, ",", Sheet2, "A1")
Set Rng = Nothing
MsgBox "Done", vbInformation, "GPE"
End Sub