Sub OkExcel() Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim i As Integer Dim SQL As String, cnnStr As String, sFileName As String Dim ws As Worksheet
Set ws = Worksheets("Sheet3") sFileName = ThisWorkbook.FullName
cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & sFileName SQL = "SELECT o.姓名, o.身份证号,t.账号, o.金额 FROM [Sheet1$] AS o INNER JOIN [Sheet2$] AS t ON (o.身份证号 = t.身份证号) AND (o.姓名 = t.姓名)" cnn.Open cnnStr rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic ws.Activate ws.Cells.Clear For i = 1 To rs.Fields.Count ws.Cells(1, i) = rs.Fields(i - 1).Name Next ws.Range("A2").CopyFromRecordset rs rs.Close: cnn.Close Set rs = Nothing: Set cnn = Nothing: Set ws = Nothing End Sub