|
Option Explicit
Sub test1()
Dim ar, i&, j&, cnn As Object, rst As Object, strSQL$, strPath$, strAddress$, strJoin$
Set cnn = CreateObject("ADODB.Connection")
strPath = ThisWorkbook.FullName
Select Case Application.Version * 1
Case Is <= 11
cnn.Open "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties='Excel 8.0;HDR=YES;IMEX=0'"
Case Is >= 12
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties='Excel 12.0;HDR=YES;IMEX=0'"
End Select
With [A1].CurrentRegion
strAddress = .Address(0, 0)
ar = .Value
For j = 3 To UBound(ar, 2)
strJoin = strJoin & "," & "AVG(" & ar(1, j) & ") as " & ar(1, j)
Next j
End With
strSQL = "SELECT 姓名" & strJoin & " FROM [sheet1$" & strAddress & "] GROUP BY 姓名"
Set rst = cnn.Execute(strSQL)
With ThisWorkbook.Sheets(2)
.Cells.Clear
For i = 0 To rst.Fields.Count - 1
.Cells(1, i + 1) = rst.Fields(i).Name
Next i
.Range("A2").CopyFromRecordset rst
.Cells.EntireColumn.AutoFit
.Activate
End With
rst.Close: cnn.Close
Set rst = Nothing: Set cnn = Nothing
Beep
End Sub
|
|