Sub Tonghop()
Dim cnn As Object, rst As Object, lSQL As String
Set cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.recordset")
Filename = Application.ThisWorkbook.FullName
If Val(Application.Version) < 12 Then
With cnn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Filename & ";" & _
"Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
.Open
End With
Else
With cnn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Filename & ";" & _
"Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
.Open
End With
End If
lSQL = "Select F2,F3,F4,F5,F6 From " _
& "(Select F2,F3,F4,F5,F6 from " _
& "(Select F2,F3,F4,F5,F6,F2 as o " _
& "From [Dovui$A2:F16] as a " _
& " Union all " _
& "Select '',f3 & ' Total','',Sum(F5),Sum(F6),'' " _
& "From [Dovui$A2:F16] as a " _
& "Group by F3) as a) " _
& "Union all " _
& "Select '','zGrand Total:','',Sum(F5),Sum(F6) " _
& "From [Dovui$A2:F16]" _
& "Order by f3,f2"
rst.Open lSQL, cnn, 3, 1
Sheet1.[J2:N65536].CurrentRegion.ClearContents
Sheet2.[J2].CopyFromRecordset rst
rst.Close: Set rst = Nothing
cnn.Close: Set cnn = Nothing
End Sub