您可以使用 ADO 和 Excel 做很多事情。假设您的工作表名称是 1Hour、4Hour 和 24Hour。
''http://support.microsoft.com/kb/257819
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.
strFile = ActiveWorkbook.FullName
''HDR=Yes, the names in the first row of the range
''are used as field (column) names.
''
''This is the ACE / Excel 2007/10 connection string, you can get more
''here : http://www.connectionstrings.com/excel
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "TRANSFORM Sum(ValX) As SumVal " _
& "SELECT GeneID FROM ( " _
& "SELECT GeneID, '01Hour' As TimeX, Value1 As ValX " _
& "FROM [1Hour$] " _
& "UNION ALL " _
& "SELECT GeneID, '04Hour' As TimeX, Value2 As ValX " _
& "FROM [4Hour$] " _
& "UNION ALL " _
& "SELECT GeneID, '24Hour' As TimeX, Value3 As ValX " _
& "FROM [24Hour$] ) t " _
& "GROUP BY GeneID " _
& "PIVOT TimeX"
rs.Open strSQL, cn, 3, 3
''Pick a suitable empty worksheet for the results
With Worksheets("Sheet4")
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 1) = rs.Fields(i).Name
Next
.Cells(2, 1).CopyFromRecordset rs
End With
''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
可以使用 Jet/ACE SQL。
基础 Microsoft Jet SQL for Access 2000
中级 Microsoft Jet SQL for Access 2000
高级 Microsoft Jet SQL for Access 2000