|
tianyunjie 发表于 2012-11-29 17:22
有个小问题,汇总表汇总完数据后,汇总表中的内容排序发生了变化,能不能不做改变呢?
ADO联合查询做不到,ADO数组+字典请测试:- Sub Macro1()
- Dim cnn As Object, rs As Object, SQL$, p$, f$, r&, n&
- Dim arr, brr(), i&, j&, d As Object
- Set d = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- Set cnn = CreateObject("ADODB.Connection")
- p = ThisWorkbook.Path & ""
- f = Dir(p & "*.xls")
- Do While f <> ""
- If f <> ThisWorkbook.Name Then
- n = n + 1
- If n = 1 Then
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & p & f
- Set rs = cnn.Execute("[Sheet1$]")
- ReDim brr(60000, rs.Fields.Count - 1)
- For i = 0 To rs.Fields.Count - 1
- brr(0, i) = rs.Fields(i).Name
- Next
- arr = rs.GetRows
- rs.Close
- Set rs = Nothing
- Else
- SQL = "select * from [Excel 8.0;Database=" & p & f & "].[Sheet1$]"
- arr = cnn.Execute(SQL).GetRows
- End If
- For j = 0 To UBound(arr, 2)
- If Not d.Exists(arr(0, j)) Then
- r = r + 1
- d(arr(0, j)) = r
- For i = 0 To UBound(arr)
- brr(r, i) = arr(i, j)
- Next
- Else
- For i = 1 To UBound(arr)
- brr(d(arr(0, j)), i) = brr(d(arr(0, j)), i) + arr(i, j)
- Next
- End If
- Next
- End If
- f = Dir()
- Loop
- cnn.Close
- Set cnn = Nothing
- Cells.ClearContents
- [a1].Resize(r + 1, i) = brr
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|