Chào các bạn,
Mình có dữ liệu giống hệt form nhau từ các sheet và mình muốn tổng hợp về một sheet duy nhất như file đính kèm, cho mình hỏi phải dùng macro hay có cách nào đơn giản hơn ko và nếu dùng macro thì cho mình xin cái code
Thanks
Dùng code như sau nhé:
[GPECODE=sql]Sub GopSheet_HLMT()
Dim cn As Object, rst As Object, cat As Object, tbl As Object, str$, str1 As String
Set cn = CreateObject("ADODB.Connection")
Set cat = CreateObject("ADOX.Catalog")
Set tbl = CreateObject("ADOX.Table")
Set rst = CreateObject("ADODB.Recordset")
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & _
";Extended Properties=""Excel 8.0;HDR=Yes;"";"
.Open
End With
cat.ActiveConnection = cn
For Each tbl In cat.Tables
If Right(Replace(tbl.Name, "'", ""), 1) = "$" And Left(tbl.Name, 4) <> "Tong" Then
str = str & " union all SELECT * from [" & Replace(Replace(tbl.Name, "$", ""), "'", "") & "$] "
str1 = Right(str, Len(str) - 10)
End If
Next
With rst
.ActiveConnection = cn
.Open str1
End With
With Sheets("Tong hop")
.[A2:C65000].ClearContents
.[A2].CopyFromRecordset rst
End With
rst.Close: Set rst = Nothing
cn.Close: Set cn = Nothing
Set cat = Nothing: Set tbl = Nothing
End Sub[/GPECODE]