|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub ADO()
- Dim Fso As Object, File As Object, cnn As Object, SQL$, m&, n&, arr, brr(1 To 100000, -1 To 2), i&, j&
- Application.ScreenUpdating = False
- Set d = CreateObject("scripting.dictionary")
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set cnn = CreateObject("adodb.connection")
- GZB = InputBox("请输入: 要提取的工作表名称,输入“*”为提取所有工作表。")
- QYFW = InputBox("请输入: 要提取的区域,输入“*”为提取所有有数据的区域")
- For Each File In Fso.GetFolder(CreateObject("scripting.filesystemobject").GetFolder(ThisWorkbook.Path) & "\分表").Files
- If File.Name Like "*.xls*" Then
- n = n + 1
- If n = 1 Then
- cnn.Open "provider=microsoft.Ace.oledb.12.0;extended properties=excel 12.0;data source=" & File
- SQL = "select * from [" & GZB & "$" & QYFW & "]"
- Else
- SQL = "select * from [Excel 12.0;Database=" & File & ";].[" & GZB & "$" & QYFW & "]"
- End If
- arr = cnn.Execute(SQL).GetRows
- brr(m + 1, -1) = n
- For i = 0 To UBound(arr, 2)
- If IsNull(arr(0, i)) Then Exit For
- m = m + 1
- For j = 0 To 2
- brr(m, j) = arr(j, i)
- Next
- Next
- End If
- Next
- ActiveSheet.UsedRange.Offset(2).ClearContents
- Range("A3").Resize(m, 3) = brr
- cnn.Close
- Set cnn = Nothing
- Set File = Nothing
- Set Fso = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码
赵老师,我修改了一下您的代码,想自己自定义工作表名称和提取区域。但是我是初学者,改了一半,后面不会改了。您能帮我看一下吗 |
|