|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
张雄友 发表于 2014-12-17 22:52
一下子还没摸索出来。 - Sub ADO加字典()
- Dim cnn As Object, rs As Object, rst As Object, SQL$, Mypath$, MyFile$, s$
- Dim d As Object, a, arr, brr$(), i&, fn$, fnws$, ma%
- Dim Fso As Object, Folder As Object, arrf$(), mf&
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Folder = Fso.GetFolder(ThisWorkbook.Path)
- Call GetFiles(Folder, arrf, mf)
- Set d = CreateObject("scripting.dictionary")
- For l = 1 To mf
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='Excel 12.0;hdr=no';Data Source=" & arrf(1, l)
- Set rs = cnn.OpenSchema(20)
- fn = arrf(2, l)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- SQL = "select f1 from [" & s & "] where f1 is not null"
- Set rst = cnn.Execute(SQL)
- If Not rst.EOF Then
- arr = rst.GetRows
- fnws = fn & ":" & Replace(s, "$", "")
- For i = 0 To UBound(arr, 2)
- If Not d.Exists(arr(0, i)) Then
- d(arr(0, i)) = fnws
- Else
- If InStr("," & d(arr(0, i)) & ",", "," & fnws & ",") = 0 Then d(arr(0, i)) = d(arr(0, i)) & "," & fnws
- End If
- Next
- End If
- End If
- End If
- rs.MoveNext
- Loop
- Next
- arr = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
- ReDim brr(1 To UBound(arr), 10000)
- For i = 1 To UBound(arr)
- s = d(arr(i, 1))
- If InStr(s, ",") Then
- a = Split(s, ",")
- For j = 0 To UBound(a)
- brr(i, j) = a(j)
- Next
- If j > ma Then ma = j
- Else
- brr(i, 0) = s
- End If
- Next
- [a1].CurrentRegion.Offset(1, 1).ClearContents
- [b2].Resize(i - 1, ma + 1) = brr
- rs.Close
- rst.Close
- Set rs = Nothing
- Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
- Set Folder = Nothing
- Set Fso = Nothing
- End Sub
- Sub GetFiles(ByVal Folder As Object, ByRef arrf$(), ByRef mf&)
- Dim SubFolder As Object
- Dim File As Object
- If Folder.Path <> ThisWorkbook.Path Then
- For Each File In Folder.Files
- If File.Name Like "*.xls*" Then
- mf = mf + 1
- ReDim Preserve arrf(1 To 2, 1 To mf)
- arrf(1, mf) = File
- arrf(2, mf) = "'" & Replace(File.Name, ".xlsx", "")
- End If
- Next
- End If
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder, arrf, mf)
- Next
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|