Sub TongHop()
Dim cnn As Object, lsSQL As String, lrs As Object ', Fname
Dim Fso As Object, Link As String ', shNameNguon, i As Long
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cnn = CreateObject("ADODB.Connection")
Set lrs = CreateObject("ADODB.Recordset")
'shNameNguon = Array("aaa")
Application.ScreenUpdating = False
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Microsoft Excel Files", "*.xls; *.xlsx; *.xlsb; *.xlsm", 1
If .Show = -1 Then
Link = .InitialFileName
Else
MsgBox "Ban da khong chon tong hop", vbInformation, "Thông Báo"
Exit Sub
End If
'For Each Fname In .SelectedItems
With cnn
If Val(Application.Version) < 12 Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Fname & ";Extended Properties=""Excel 8.0;HDR=No"";"
Else
' Fname = .SelectedItems
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1) & ";Extended Properties=""Excel 12.0;HDR=No"";"
End If
.Open
'For i = 0 To UBound(shNameNguon)
lsSQL = "SELECT * FROM [aaa$A2:AC65536]"
lrs.Open lsSQL, cnn, 3, 1
Sheets("aaa").Range("A2:AC65536").ClearContents
Sheets("aaa").Range("A2").CopyFromRecordset lrs
lrs.Close
'Next
End With
'Next
End With
Application.ScreenUpdating = True
cnn.Close
Set lrs = Nothing
Set cnn = Nothing
End Sub