|
楼主 |
发表于 2021-3-4 10:24
|
显示全部楼层
- Sub AwTest2222()
- Dim i%, j%, iRow%, s&, sr$, ws As Worksheet, arr, d As Object
- Set d = CreateObject("Scripting.Dictionary") '创建字典对象,并把字典对象赋给变量d。这是最常用的一句代码。所谓的“后期绑定”。用了这句代码就不用先引用c:\windows\system32\scrrun.dll了
- For Each ws In Sheets
- If ws.Name <> "汇总表" And ws.Name <> "总表" And ws.Name <> "汇总" And ws.Name <> "求和" Then
- arr = ws.[a1].CurrentRegion '本示例假定在工作表 Sheet1 中有一个包含标题行的表。本示例选定该表,但不选定标题行
- For i = 2 To UBound(arr)
- d(ws.Name & "|" & arr(i, 3)) = ""
- Next
- End If
- Next
- arr = Sheets("汇总表").[a1].CurrentRegion
- For i = 2 To UBound(arr)
- If Not d.exists(arr(i, 4) & "|" & arr(i, 3)) Then 'arr(i, 4)/arr(i,3)值 的内容不存在D字典中”,那么执行下面的语句
- For Each ws In Sheets
- If ws.Name = arr(i, 4) Then '如果D列班级等于表名,然后
- iRow = ws.Cells(Rows.Count, 3).End(3).Row '整体意思是第三列姓名最后一个有数据的行的行号
- For j = 1 To 4
- ws.Cells(iRow + 1, j) = arr(i, j) '如果没有则新建一行等于新数组
- Next
- End If
- Next
- End If
- Next
- For i = 2 To UBound(arr) '第2行到最后一行
- For j = 5 To UBound(arr, 2)
- If arr(i, j) > 0 Then '如果(arr(i, 1))单元格的内容长度大于0
- sr = arr(i, 4) & "|" & arr(i, 3) & "|" & arr(1, j)
- d(sr) = arr(i, j) '就将arr(i, 1))单元格的内容做为关建字加入字典
- End If
- Next
- Next
- For Each ws In Sheets
- If ws.Name <> "汇总表" And ws.Name <> "总表" And ws.Name <> "汇总" And ws.Name <> "求和" Then
- arr = ws.[a1].CurrentRegion
- For i = 2 To UBound(arr)
- s = 0 '累加参数初始化
- For j = 5 To 11 'E-N列
- sr = ws.Name & "|" & arr(i, 3) & "|" & arr(1, j)
- If d.exists(sr) Then arr(i, j) = d(sr)
- If j < 12 Then s = 0 + Val(arr(i, j)) '''''''''''''' '' '每一个表的行第5列到13列数据数据累加给s
- Next ''''''''''''''''''''''''''''''''''''''''''
- arr(i, 12) = s '''''''''''此上下4行修改了
- arr(i, 14) = s - arr(i, 13) ''''''''''''''''
-
- Next
- ws.[a1].CurrentRegion = arr
- End If
- Next
- End Sub
复制代码
数据是可以复制进去表格,但是依然是合计算不了 |
|