|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
请参考:- Sub Macro1()
- Dim cnn As Object, SQL$, p$, f$, m&, arr, brr(), i&, j&, r, c, dr As Object, dc As Object
- Set dr = CreateObject("scripting.dictionary")
- Set dc = CreateObject("scripting.dictionary")
- arr = [a1].CurrentRegion
- For i = 2 To UBound(arr)
- dr(arr(i, 1) & ".xlsx") = i
- Next
- For j = 2 To UBound(arr, 2)
- dc(arr(1, j)) = j
- Next
- ReDim brr(2 To i - 1, 2 To j - 1)
- p = ThisWorkbook.Path & "\test"
- f = Dir(p & "*.xlsx")
- Do While f <> ""
- m = m + 1
- If m = 1 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=excel 12.0;Data Source=" & p & f
- SQL = "select 货物,金额 from [" & Replace(f, ".xlsx", "$]")
- Else
- SQL = "select 货物,金额 from [Excel 12.0;Database=" & p & f & ";].[" & Replace(f, ".xlsx", "$]")
- End If
- arr = cnn.Execute(SQL).GetRows
- r = dr(f)
- If r <> "" Then
- For i = 0 To UBound(arr, 2)
- c = dc(arr(0, i))
- If c <> "" Then brr(r, c) = arr(1, i)
- Next
- End If
- f = Dir()
- Loop
- [a1].CurrentRegion.Offset(1, 1).ClearContents
- [b2].Resize(UBound(brr) - 1, UBound(brr, 2) - 1) = brr
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|