Mình có 1 file tổng hợp xà lan ở các bãi, mỗi bãi nhập trên 1 sheet, ở đó có 1 sheet tổng hợp mình đã làm nút cập nhật theo hướng dẫn của anh Bate để khi nhấn nó sẽ tự cập nhật dữ liệu ở các sheet kia về sheet tổng hợp, nhưng khi mình thêm 1 sheet mới thì khi cập nhật nó lại ra 1 khoảng trắng ở giũa.. mong mọi người chỉ giáo :U
Bạn chỉnh lại code như sau:
[GPECODE=sql]Sub GopSheet_HLMT()
Dim cn As Object, rst As Object, cat As Object, tbl As Object, str$, arr As Variant, i As Integer
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) = "$" Then str = str & Replace(Replace(tbl.Name, "$", ""), "'", "") & ";"
Next
arr = Split(str, ";")
For i = 0 To UBound(arr) - 1
Dim str1, str2 As String
If arr(i) <> "TONG" Then
str1 = str1 & " union all SELECT * from [" & arr(i) & "$B12:M65000] where [GRMT] is not null"
str2 = Right(str1, Len(str1) - 10)
End If
Next
With rst
.ActiveConnection = cn
.Open str2
End With
With Sheets("TONG")
.[B12:M65000].ClearContents
.[B12].CopyFromRecordset rst
End With
rst.Close: Set rst = Nothing
cn.Close: Set cn = Nothing
Set cat = Nothing: Set tbl = Nothing: Erase arr
End Sub
[/GPECODE]
P/S: ANh Ba Tê nay sử dụng ADO luôn ta....