|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
ashengz86 发表于 2014-12-16 14:26
老师,首先万分感谢!请见附件我现在想实现多文件夹多 多工作表sheet查找,并显示sheet名!
输出太复杂了,改为文件名:表名:- 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$, 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
- For i = 0 To UBound(arr, 2)
- If Not d.Exists(arr(0, i)) Then d(arr(0, i)) = fn & ":" & Replace(s, "$", "") Else d(arr(0, i)) = d(arr(0, i)) & "," & fn & ":" & Replace(s, "$", "")
- 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
- 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 "*.xlsx" 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
复制代码 |
|