|
请参考:
- Sub ADO查询()
- Dim cnn As Object, rs As Object, rst As Object, SQL$, MyPath$, MyName$, m&, r&, s$, t$
- Application.ScreenUpdating = False
- r = 2
- Range("I2:N1000") = ""
- t = ThisWorkbook.Worksheets(1).Range("A1").Value
- MyPath = ThisWorkbook.Path & "\分表"
- MyName = Dir(MyPath & "*.xlsx")
- Do While MyName <> ""
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyName
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- SQL = "select 月份,出生地,学历,职位,数量 from [Excel 12.0;DATABASE=" & MyPath & MyName & "].[" & s & "A1:F100] where 姓名 = '" & t & "'order by 月份"
- Set rst = CreateObject("ADODB.recordset")
- rst.Open SQL, cnn, 1, 3
- If rst.RecordCount Then
- Range("i" & r).CopyFromRecordset rst
- r = r + rst.RecordCount
- End If
- End If
- End If
- rs.MoveNext
- Loop
- MyName = Dir()
- Loop
- rs.Close
- rst.Close
- cnn.Close
- Set rs = Nothing
- Set rst = Nothing
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|