Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2585|回复: 6

[已解决]汇总数据

[复制链接]
发表于 2017-4-24 07:42 | 显示全部楼层 |阅读模式


  要求:现在是4月,要求将表“IODC”和表"EFPR"3月的数据汇总到,“汇总”表里
  备注:有料号相同项就合并计算,如果是空格删除


请各位老师帮帮忙,谢谢。
  
[/td] [/tr]
最佳答案
2017-4-24 09:30
  1. Sub 汇总()
  2.     yf = Month(Date) - 1   '上一月
  3.     Set d = CreateObject("scripting.dictionary")
  4.     For k = 1 To 2
  5.         With Sheets(k)
  6.             r = .UsedRange.Find("料号").Row
  7.             If k = 1 Then
  8.                 For c = .Cells(r, 256).End(xlToLeft).Column To 2 Step -1
  9.                     If Month(.Cells(r, c)) = yf Then Exit For
  10.                 Next
  11.             Else
  12.                 For c = .Cells(r, 256).End(xlToLeft).Column To 2 Step -1
  13.                     If .Cells(r, c) = yf & "月" Then Exit For
  14.                 Next
  15.             End If
  16.             For i = r + 1 To .[a65536].End(3).Row
  17.                 x = .Cells(i, 1)
  18.                 d(x) = d(x) + .Cells(i, c)
  19.             Next
  20.         End With
  21.     Next
  22.     For Each x In d.keys
  23.         If d(x) = 0 Then d.Remove (x)
  24.     Next
  25.     [a2].Resize(d.Count, 2) = Application.Transpose(Array(d.keys, d.items))
  26.     [a2].Resize(d.Count, 2).Sort key1:=[b2], order1:=xlDescending
  27. End Sub
复制代码

合并计算.zip

15.43 KB, 下载次数: 6

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-4-24 09:30 | 显示全部楼层    本楼为最佳答案   
  1. Sub 汇总()
  2.     yf = Month(Date) - 1   '上一月
  3.     Set d = CreateObject("scripting.dictionary")
  4.     For k = 1 To 2
  5.         With Sheets(k)
  6.             r = .UsedRange.Find("料号").Row
  7.             If k = 1 Then
  8.                 For c = .Cells(r, 256).End(xlToLeft).Column To 2 Step -1
  9.                     If Month(.Cells(r, c)) = yf Then Exit For
  10.                 Next
  11.             Else
  12.                 For c = .Cells(r, 256).End(xlToLeft).Column To 2 Step -1
  13.                     If .Cells(r, c) = yf & "月" Then Exit For
  14.                 Next
  15.             End If
  16.             For i = r + 1 To .[a65536].End(3).Row
  17.                 x = .Cells(i, 1)
  18.                 d(x) = d(x) + .Cells(i, c)
  19.             Next
  20.         End With
  21.     Next
  22.     For Each x In d.keys
  23.         If d(x) = 0 Then d.Remove (x)
  24.     Next
  25.     [a2].Resize(d.Count, 2) = Application.Transpose(Array(d.keys, d.items))
  26.     [a2].Resize(d.Count, 2).Sort key1:=[b2], order1:=xlDescending
  27. End Sub
复制代码

合并计算.rar

25.64 KB, 下载次数: 16

评分

参与人数 1 +1 收起 理由
王英wang + 1 很给力

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2017-4-24 09:53 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2017-4-24 10:30 | 显示全部楼层

你好,我把代码放到其它工作表就下标越界,是怎么回事?
我要汇总工作表3,4,我也改啦 for 3 to 4,但是怎么执行不了呢
回复

使用道具 举报

发表于 2017-4-24 10:33 | 显示全部楼层
sheets(k)是工作表的索引号,for k=3 to 4 ,那么要引用的表就是第3,第4张工作表。
另外由于两张表的格式不同,所以要注意表和代码相对应(表1里的“3月”是日期,表2里的“3月”是字符串)
回复

使用道具 举报

 楼主| 发表于 2017-4-24 10:48 | 显示全部楼层
grf1973 发表于 2017-4-24 10:33
sheets(k)是工作表的索引号,for k=3 to 4 ,那么要引用的表就是第3,第4张工作表。
另外由于两张表的格式 ...

非常感谢,谢谢{:1112:}
回复

使用道具 举报

发表于 2017-4-30 22:15 | 显示全部楼层
下载学习学习了,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-23 18:53 , Processed in 0.519031 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表