Sub getDataFromFile()
Dim arr
' Duong` dan den File
Const kFile As String = "\xpart_0.txt"
'getTxT kFile, Sheet1.[A1], "|"
'neu' dung` getTxT kFile, Sheet1.[A1] thì bo cac' hàng duoi nay` di
aArr = getTxT(kFile, , "|")
[A1].Resize(UBound(aArr), UBound(aArr, 2)) = aArr
End Sub
Function getTxT(sFile As String, Optional ByVal rng As Range, Optional ByVal punc As string = " ") As Variant
Dim objStream, strAdo, arr() As String, dArr() As Variant, xArr() As String
Dim i As Integer, j As Integer, k As Integer: k = 0
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 2 'Stream type
.Charset = "utf-8" 'or utf-16 etc
.Open
.LoadFromFile sFile
.LineSeparator = 13
strAdo = .ReadText(-2)
.Close
End With
arr = Split(strAdo, Chr(10))
For i = 0 To UBound(arr)
xArr = Split(Application.WorksheetFunction.Clean(Trim(arr(i))), punc)
If UBound(xArr) > k Then k = UBound(xArr) + 1
ReDim Preserve dArr(1 To UBound(arr) + 1, 1 To k)
For j = 0 To UBound(xArr)
dArr(i + 1, j + 1) = Application.WorksheetFunction.Clean(Trim(xArr(j)))
Next j
Next i
getTxT = dArr
Set objStream = Nothing
If rng Is Nothing Or IsArray(rng) Then Exit Function
rng.Resize(UBound(dArr), UBound(dArr, 2)) = dArr
End Function