Em xin có 1 câu hỏi thế này ạ.
Giờ em đang có 3 file excel và để lưu chung vào 1 địa chỉ là: D/QLC/DANH SACH
Em đã gửi danh sách cả 4 file ở tập new folder và trong đó em muốn lấy tất cả cột thành tiền trong 3 file về file tổng hợp theo mẫu ở tổng hợp, số liệu sẽ tương ứng với ngày phát sinh ạ.
Xin cảm ơn anh chị
Sử dụng code này:
[GPECODE=sql]Sub GopFile()
Dim cn As Object, adoRS As Object, cat As Object, tbl As Object, FileItem As Object
Dim strTableName As String, i As Integer, endR As Integer, strFile As String
Set cn = CreateObject("ADODB.Connection")
Set adoRS = CreateObject("ADODB.Recordset")
Set cat = CreateObject("ADOX.Catalog")
Cells.ClearContents
For Each FileItem In CreateObject("Scripting.FileSystemObject").GetFolder(BrowseForFolder).Files
If FileItem.Path Like "*.xls" Then
strFile = FileItem.Path
strTableName = FileItem.Name
If strFile <> ThisWorkbook.FullName Then
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFile & _
";Extended Properties=""Excel 8.0;HDR=Yes;HDR=No;IMEX=1"";"
.Open
End With
cat.ActiveConnection = cn
If (Right(cat.Tables(0).Name, 1) = "$") Or (Right(cat.Tables(0).Name, 2) = "$'") Then
strTableName = Replace(cat.Tables(0).Name, "'", "")
MsgBox strTableName
With adoRS
.ActiveConnection = cn
.Open "SELECT '" & strFile & "' as [Duong Dan],'" & strTableName & "' as [Ten Sheet], * FROM [" _
& strTableName & "A1:E5000]"
endR = Range("A65000").End(xlUp).Row + 1
Application.ScreenUpdating = False
Range("A" & endR).CopyFromRecordset adoRS
Application.ScreenUpdating = True
.Close
cn.Close
End With
End If
End If
End If
Next
Set cn = Nothing: Set adoRS = Nothing
Set adoRS = Nothing: Set cat = Nothing: Set tbl = Nothing
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Vui long chon folder co chua file ma ban can gop.", 0, OpenAt)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
BrowseForFolder = False
End Function
[/GPECODE]
Lưu ý là các file cần gộp chỉ để 1 sheet duy nhất nhé.