|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
simon_k0219 发表于 2014-4-12 22:11
附件如下:
- Sub Macro1()
- Dim Fso As Object, Folder As Object, arrf$(), mf&
- Dim cnn As Object, SQL$
- Dim objWMI As Object
- Const HKEY_LOCAL_MACHINE = &H80000002
- Set objWMI = GetObject("winmgmts:\\.\root\default:StdRegProv")
- objWMI.SetDWORDValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines\Excel", "TypeGuessRows", 60000
- Application.ScreenUpdating = False
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Folder = Fso.GetFolder(ThisWorkbook.Path)
- Call GetFiles(Folder, arrf, mf)
- ActiveSheet.UsedRange.Offset(1).ClearContents
- For i = 1 To mf
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;imex=1';Data Source=" & arrf(i)
- SQL = "select '" & cnn.Execute("[姓名$e7:e7]").Fields(0).Name & "',* from [费用$a8:o] where 费用名称='苹果' or 费用名称 like '%西瓜%'"
- Range("h65536").End(xlUp).Offset(1, -7).CopyFromRecordset cnn.Execute(SQL)
- Next
- End Sub
- Sub GetFiles(ByVal Folder As Object, ByRef arrf$(), ByRef mf&)
- Dim SubFolder As Object
- Dim File As Object
- For Each File In Folder.Files
- If File.Name Like "*.xls" Then
- If File.Name <> ThisWorkbook.Name Then
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = File
- End If
- End If
- Next
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder, arrf, mf)
- Next
- End Sub
复制代码 |
|