我也来一个,SQL的! Public Sub hb() Application.ScreenUpdating = False '关闭屏幕更新 Application.EnableEvents = False '关闭事件响应 'Application.Interactive = False '禁止所有的键盘输入和鼠标输入' Dim conn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim sql As String Set conn = New ADODB.Connection Set rst = New ADODB.Recordset rq = Format(Now(), "yyyy-m-d") conn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source = " & ThisWorkbook.FullName sql = "select A.姓名,max(A.工龄) AS 工龄,max(A.工资) AS 工资,max(A.补贴) as 补贴,max(A.住房费) as 住房费,max(A.Expr1005) as 伙食费 from " & _ "(select 姓名,工龄,工资,补贴,住房费,'' from [数据表1$] " & _ "Union " & _ " select 姓名,工龄,工资,'','',伙食费 from [数据表2$]) A GROUP BY A.姓名" rst.Open sql, conn, adOpenKeyset, adLockOptimistic h = rst.RecordCount Sheets("汇总").Select For Each Field In rst.Fields Sheets("汇总").[a1].Offset(0, I) = Field.Name I = I + 1 Next rowh = Sheets("汇总").Range("a65000").End(xlUp).Row Rows("2:" & rowh).Select Selection.ClearContents rowh = Sheets("汇总").Range("a65000").End(xlUp).Row Sheets("汇总").Range("A" & rowh).Offset(1, 0).CopyFromRecordset rst Range("A2").Select conn.Close Set conn = Nothing Application.Interactive = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub 非常棒!呵呵 |