|
第一种: 多簿多表不等复合行,列的字典法 ,请测试。- '引用 Microsoft Scripting Runtime 'C:\WINDOWS\system32\scrrun.dll
- Sub 汇总()
- Dim xDic As New dictionary, yDic As New dictionary
- Dim filePath As String, fileName As String
- Dim eBook As Workbook, eSheet As Worksheet
- Dim xRng As Range, yRng As Range, zRng As Range, rng As Range
- Dim rngArr() As Variant, arr() As Variant
- Dim x As Integer, y As Integer, i As Integer, j As Integer
- Dim m As Integer, n As Integer, Index As Integer
- t = Timer
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- filePath = ThisWorkbook.Path & "\各部门销售"
- fileName = Dir(filePath & "\*.xls")
- Do While Len(fileName) > 0
- Set eBook = Workbooks.Open(filePath & "" & fileName)
- For Each eSheet In eBook.Sheets
- If eSheet.Name Like "*销量" Then
- Index = Index + 1
- With eSheet ' 合*计 预防“ 合 计 ”这类非规范写法
- Set xRng = .Cells.Find(What:="合*计", After:=.Range("a1"))
- Set yRng = .Cells.FindNext(After:=xRng)
- ReDim Preserve rngArr(1 To Index)
- rngArr(Index) = .Range(.Cells(xRng.Row, yRng.Column), .Cells(yRng.Row - 1, xRng.Column - 1))
- End With
- End If
- Next
- eBook.Close False
- fileName = Dir()
- Loop
-
- For Index = 1 To UBound(rngArr)
- For i = 3 To UBound(rngArr(Index))
- If rngArr(Index)(i, 1) = "" Then rngArr(Index)(i, 1) = rngArr(Index)(i - 1, 1)
- If Not xDic.Exists(rngArr(Index)(i, 1) & "|" & rngArr(Index)(i, 2)) Then
- x = x + 1
- xDic(rngArr(Index)(i, 1) & "|" & rngArr(Index)(i, 2)) = x
- End If
- Next
- For i = 3 To UBound(rngArr(Index), 2)
- If rngArr(Index)(1, i) = "" Then rngArr(Index)(1, i) = rngArr(Index)(1, i - 1)
- If Not yDic.Exists(rngArr(Index)(1, i) & "|" & rngArr(Index)(2, i)) Then
- y = y + 1
- yDic(rngArr(Index)(1, i) & "|" & rngArr(Index)(2, i)) = y
- End If
- Next
- Next
- ReDim arr(1 To x, 1 To y)
- For Index = 1 To UBound(rngArr)
- For i = 3 To UBound(rngArr(Index))
- For j = 3 To UBound(rngArr(Index), 2)
- m = xDic(rngArr(Index)(i, 1) & "|" & rngArr(Index)(i, 2))
- n = yDic(rngArr(Index)(1, j) & "|" & rngArr(Index)(2, j))
- arr(m, n) = arr(m, n) + rngArr(Index)(i, j)
- Next
- Next
- Next
- With Sheets("总表")
- .Cells.Delete
- Set xRng = .Range("b4").Resize(xDic.Count, 1)
- xRng = Application.Transpose(xDic.Keys)
- For Each rng In xRng
- rng.Resize(, 2) = Split(rng, "|")
- Next
- Set yRng = .Range("d2").Resize(1, yDic.Count)
- yRng = yDic.Keys
- For Each rng In yRng
- rng.Resize(2) = Application.Transpose(Split(rng, "|"))
- Next
- .Range("d4").Resize(xDic.Count, yDic.Count) = arr
- Set zRng = .Range("d2").Resize(xDic.Count + 2, yDic.Count)
- zRng.Sort Key1:=.Range("D2"), Orientation:=xlSortRows
- Set zRng = .Range("b4").Resize(xDic.Count, yDic.Count + 2)
- zRng.Sort Key1:=.Range("b4"), Orientation:=xlSortColumns
- x = 0
- For Each rng In xRng
- If rng.Text <> rng.Offset(1).Text Then
- If x > 0 Then rng.Offset(-x).Resize(x + 1).Merge
- x = 0
- Else
- x = x + 1
- End If
- Next
- y = 0
- For Each rng In yRng
- If rng.Text <> rng.Offset(, 1).Text Then
- If y > 0 Then rng.Offset(, -y).Resize(1, y + 1).Merge
- y = 0
- Else
- y = y + 1
- End If
- Next
- .Range("b2:b3").Merge
- .Range("c2:c3").Merge
- .Range("b2") = "省份"
- .Range("c2") = "销售网点"
- With .Cells(2, yDic.Count + 4).Resize(2)
- .Merge
- .Value = "合计"
- End With
- With .Cells(xDic.Count + 4, 2).Resize(, 2)
- .Merge
- .Value = "合计"
- End With
- Set rng = Range("b2").Resize(xDic.Count + 3, yDic.Count + 3)
- rng.Borders.LineStyle = True
- For i = 3 To xDic.Count + 3
- rng.Cells(i, yDic.Count + 3) = "=SUM(RC[-" & yDic.Count + 3 - 3 & "]:RC[-1])"
- Next
- For i = 3 To yDic.Count + 3
- rng.Cells(xDic.Count + 3, i) = "=SUM(R[-" & xDic.Count + 3 - 3 & "]C:R[-1]C)"
- Next
- End With
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox Timer - t
- End Sub
复制代码 为上面这些代码已经用了4个多小时了
第二种: 多簿多表不等复合行,列的SQL法 ,已经解决复合 行和复合列标题(域函数),有空再做。
彭希仁:非常不错,能想到多工作薄,但离傻瓜式还有差距,你的东西只能给会VBA的人使用。
[ 本帖最后由 彭希仁 于 2010-1-29 17:32 编辑 ] |
评分
-
2
查看全部评分
-
|