Vấn đề là em có độ 10.000 file xml, tên file được đặt tên theo kiểu random do 1 chương trình nào đó tạo ra. Giờ có cách nào đưa dữ liệu 10.000 file xml đó vào cùng 1 sheet trong excel không. mà không làm thủ công add từng file 1.
3 file em up lên rồi đó. Xin cám ơn
Function GetListFile(ByVal Folder As String, ByVal Search As String, ByVal InSub As Boolean)
Dim sComm As String, tmp As String, tmpFile, Arr, sPath As String
On Error Resume Next
If Right(Folder, 1) <> "\" Then Folder = Folder & "\"
sPath = """" & Folder & "*" & Search & "*"""
With CreateObject("Scripting.FileSystemObject")
tmpFile = .GetTempName
sComm = "DIR " & sPath & " /ON /B /A-D " & IIf(InSub, "/S", " ") & " >" & tmpFile
CreateObject("Wscript.Shell").Run "cmd /u /c " & sComm, 0, True
With .OpenTextFile(tmpFile, 1, , -2)
tmp = Trim(.ReadAll)
If Right(tmp, 2) = vbCrLf Then tmp = Left(tmp, Len(tmp) - 2)
If Len(tmp) Then GetListFile = Split(tmp, vbCrLf)
.Close
End With
End With
Kill tmpFile
End Function
Sub Main()
Dim sFile As String, sFolder As String
Dim aFiles, fleItem, Target As Range
sFolder = CreateObject("Shell.Application").BrowseForFolder(0, "", 1).Self.Path
aFiles = GetListFile(sFolder, "*.xml", True)
Sheet2.UsedRange.Clear
With Application
.DisplayAlerts = False
.ScreenUpdating = False
For Each fleItem In aFiles
Set Target = Sheet2.Range("A60000").End(xlUp).Offset(1)
sFile = CStr(fleItem)
ThisWorkbook.XmlImport sFile, Nothing, True, Target
Next
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
Dựa vào 3 file của bạn, tôi tiến hành thí nghiệm bằng cách record macro quá trình import bằng tay rồi chỉnh lại code
Mã:Function GetListFile(ByVal Folder As String, ByVal Search As String, ByVal InSub As Boolean) Dim sComm As String, tmp As String, tmpFile, Arr, sPath As String On Error Resume Next If Right(Folder, 1) <> "\" Then Folder = Folder & "\" sPath = """" & Folder & "*" & Search & "*""" With CreateObject("Scripting.FileSystemObject") tmpFile = .GetTempName sComm = "DIR " & sPath & " /ON /B /A-D " & IIf(InSub, "/S", " ") & " >" & tmpFile CreateObject("Wscript.Shell").Run "cmd /u /c " & sComm, 0, True With .OpenTextFile(tmpFile, 1, , -2) tmp = Trim(.ReadAll) If Right(tmp, 2) = vbCrLf Then tmp = Left(tmp, Len(tmp) - 2) If Len(tmp) Then GetListFile = Split(tmp, vbCrLf) .Close End With End With Kill tmpFile End Function
Việc của bạn là:Mã:Sub Main() Dim sFile As String, sFolder As String Dim aFiles, fleItem, Target As Range sFolder = CreateObject("Shell.Application").BrowseForFolder(0, "", 1).Self.Path aFiles = GetListFile(sFolder, "*.xml", True) Sheet2.UsedRange.Clear With Application .DisplayAlerts = False .ScreenUpdating = False For Each fleItem In aFiles Set Target = Sheet2.Range("A60000").End(xlUp).Offset(1) sFile = CStr(fleItem) ThisWorkbook.XmlImport sFile, Nothing, True, Target Next .DisplayAlerts = True .ScreenUpdating = True End With End Sub
- Mở file tôi đính kèm dưới đây
- Bấm nút 'Run code'
- Cửa sổ Browse Folder hiện ra, bạn duyệt tới thư mục chứa file xml rồi bấm OK
- Code chạy xong, bạn sang sheet2 để xem kết quả
-------------------------
Tôi không chắc lắm về tốc độ của code nên bước đầu bạn nên để trong thư mục chừng 100 file thôi... Khi cảm thấy code chạy ổn định rồi hẳn import nhiều file