|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub getData()
Dim cnn As Object, rst As Object, p$, f$, Sql$, r%, c&, i%, s$
Set cnn = CreateObject("adodb.connection")
p = ThisWorkbook.Path & "\"
f = "工作簿2.xlsx"
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & p & f
Set rst = cnn.OpenSchema(20)
Do Until rst.EOF
If rst.Fields("TABLE_TYPE") = "TABLE" Then
s = Replace(rst("TABLE_NAME").Value, "'", "")
If Right(s, 1) = "$" Then
Sql = Sql & "Union All Select 列1,列2,列3,列4,列5,列6 From [" & s & "a3:f] "
' Sql = Sql & "Union All Select * From [" & s & "a3:f] "
End If
End If
rst.MoveNext
Loop
rst.Close
Sql = Mid(Sql, 11)
Set rst = cnn.Execute(Sql)
Dim ar, br(1 To 16888, 1 To 6), k&
ar = rst.GetRows
k = 0
For c = 0 To UBound(ar, 2)
If ar(1, c) <> "" Then
k = k + 1
For r = 0 To UBound(ar)
br(k, r + 1) = ar(r, c)
Next
End If
Next
With Me
.Cells = ""
For i = 1 To rst.Fields.Count
.[a3].Offset(0, i - 1) = rst.Fields(i - 1).Name
Next
.[a4].Resize(k, 6) = br
End With
cnn.Close
Set cnn = Nothing
Set rst = Nothing
End Sub |
|