|
- Sub t()
- Dim i&
- Dim cnn, rs, arr
- Dim myPath$, myFile$
- Dim sField$, sCdn$
- Application.ScreenUpdating = False
- myPath = ThisWorkbook.Path & ""
- myFile = Dir(myPath & "*.xls*")
-
- Range("B8:U" & Rows.Count).Clear
- arr = [B7:U7]
- For i = 1 To UBound(arr, 2)
- sField = IIf(sField = "", "[", sField & ",[") & arr(1, i) & "]"
- Next
-
- arr = Range("A7:A" & Cells(Rows.Count, 1).End(3).Row)
-
- For i = 2 To UBound(arr)
- sCdn = IIf(sCdn = "", Chr(34), sCdn & "," & Chr(34)) & arr(i, 1) & Chr(34)
- Next
-
- Set cnn = CreateObject("adodb.connection")
- Set rs = CreateObject("adodb.Recordset")
-
- Do While myFile <> ""
- If myFile <> ThisWorkbook.Name Then
-
- If Application.Version < 12 Then
- cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & myPath & myFile
- Else
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & myPath & myFile
- End If
-
- Set rs = cnn.OpenSchema(20) 'adSchemaTables
-
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- shtName = rs("TABLE_NAME").Value
-
- Sql = "SELECT " & sField & " FROM [" & shtName & "a2:x] where [姓名] in ( " & sCdn & ")"
-
- On Error Resume Next
- Cells(Rows.Count, 2).End(3).Offset(1).CopyFromRecordset cnn.Execute(Sql)
- On Error GoTo 0
-
- End If
- rs.MoveNext
- Loop
- cnn.Close
- End If
- myFile = Dir
- Loop
-
- Set rs = Nothing
- Set cnn = Nothing
-
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|