nguyenhoang_roland
Thành viên mới
![](/diendan/data/PhoToDanhHieu/gold.gif)
- Tham gia
- 5/9/11
- Bài viết
- 9
- Được thích
- 0
Xin chào toàn thể anh em GPE. Mình là thành viên mới và đang tập tành Macro để áp dụng trong công việc.
Hiện tại, mình muốn copy nội dung từ file txt sang xls bằng macro (cái này mình làm theo sự hướng dẫn trên forums ok). Nhưng mình muốn bổ sung thêm copy tên file với nội dung luôn để không bị nhầm dữ liệu khi xử lý (vì có rất nhiều file txt cùng nội dung chỉ khác tên file).
Mong mọi người giúp mình chỉnh sửa code nhé. Code mình copy và chỉnh sửa từ forums.
Sub Main()
Dim vFile, txtFile, aCols, aRows, Arr
Dim sAll As String, tmp As String
Dim fso As Object
Dim lR As Long, lC As Long, n As Long, t As Double
On Error Resume Next
vFile = Application.GetOpenFilename("Text Files, *.txt", , , , True)
If TypeName(vFile) = "Variant()" Then
t = Timer
Set fso = CreateObject("Scripting.FileSystemObject")
For Each txtFile In vFile
With fs
penTextFile(txtFile, 1)
sAll = .ReadAll
.Close
End With
aRows = Split(sAll, vbCrLf)
If Not IsArray(Arr) Then ReDim Arr(1 To UBound(aRows), 1 To UBound(vFile))
lC = lC + 1: lR = 0
For n = 0 To UBound(aRows)
tmp = CStr(aRows(n))
If Len(tmp) Then
lR = lR + 1
aCols = Split(tmp, vbTab)
Arr(lR, lC) = aCols(0)
End If
Next
Next
Set fso = Nothing
If lR Then
Range("A1").Resize(lR, lC).Value = Arr
MsgBox "Xong nhe ^_^", , Format(Timer - t, "0.000000")
End If
End If
End Sub
P/S: tên file txt muốn copy vô chung nội dung: 1806-XXXX-XX
Cảm ơn mọi người đã quan tâm và đọc tin.
Nguyenhoang_roland
Hiện tại, mình muốn copy nội dung từ file txt sang xls bằng macro (cái này mình làm theo sự hướng dẫn trên forums ok). Nhưng mình muốn bổ sung thêm copy tên file với nội dung luôn để không bị nhầm dữ liệu khi xử lý (vì có rất nhiều file txt cùng nội dung chỉ khác tên file).
Mong mọi người giúp mình chỉnh sửa code nhé. Code mình copy và chỉnh sửa từ forums.
Sub Main()
Dim vFile, txtFile, aCols, aRows, Arr
Dim sAll As String, tmp As String
Dim fso As Object
Dim lR As Long, lC As Long, n As Long, t As Double
On Error Resume Next
vFile = Application.GetOpenFilename("Text Files, *.txt", , , , True)
If TypeName(vFile) = "Variant()" Then
t = Timer
Set fso = CreateObject("Scripting.FileSystemObject")
For Each txtFile In vFile
With fs
![Er... what? o.O o.O](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f635.png)
sAll = .ReadAll
.Close
End With
aRows = Split(sAll, vbCrLf)
If Not IsArray(Arr) Then ReDim Arr(1 To UBound(aRows), 1 To UBound(vFile))
lC = lC + 1: lR = 0
For n = 0 To UBound(aRows)
tmp = CStr(aRows(n))
If Len(tmp) Then
lR = lR + 1
aCols = Split(tmp, vbTab)
Arr(lR, lC) = aCols(0)
End If
Next
Next
Set fso = Nothing
If lR Then
Range("A1").Resize(lR, lC).Value = Arr
MsgBox "Xong nhe ^_^", , Format(Timer - t, "0.000000")
End If
End If
End Sub
P/S: tên file txt muốn copy vô chung nội dung: 1806-XXXX-XX
Cảm ơn mọi người đã quan tâm và đọc tin.
Nguyenhoang_roland