|
请参考:
- Sub ADO加字典法()
- Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, c, s$, arr, brr(), i&, d As Object
- Set d = CreateObject("scripting.dictionary")
- arr = Range("A1").CurrentRegion
- For i = 2 To UBound(arr, 2)
- d("" & arr(1, i)) = i
- Next
- ReDim brr(0 To 33, 2 To i - 1)
- Set Fso = CreateObject("Scripting.FileSystemObject")
- For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
- If LCase(File.Name) Like "*.xls" And LCase(File.Name) <> ThisWorkbook.Name Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & File
- 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
- c = d(Left$(s, 7))
- If c <> "" Then
- SQL = "select * from [" & s & "E8:E41]"
- arr = cnn.Execute(SQL).GetRows
- For i = 0 To 33
- If Not IsNull(arr(0, i)) Then brr(i, c) = arr(0, i)
- Next
- End If
- End If
- End If
- rs.MoveNext
- Loop
- End If
- Next
- Range("b2").Resize(34, UBound(brr, 2) - 1) = brr
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set File = Nothing
- Set Fso = Nothing
- End Sub
复制代码 |
|