Excel精英培训网

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

[已解决]请求不重复字段的数字累加,然后放到行或列

[复制链接]
发表于 2011-10-4 08:24 | 显示全部楼层 |阅读模式
请求代码:把不重复字段的数字累加,然后放到行或列
最佳答案
2011-10-4 11:42
Sub aa()
Dim x As Integer
Dim d As Object
Dim a
Dim b

Set d = CreateObject("Scripting.Dictionary")
For x = 1 To Range("A65536").End(xlUp).Row
    If Cells(x, 2) <> "" Then
        d(Cells(x, 2).Value) = Cells(x, 1).Value + d(Cells(x, 2).Value)
    End If
Next x
a = d.Items
b = d.keys
For x = 0 To d.Count - 1
Sheets("sheet2").Cells(x + 1, 1) = b(x)
Sheets("sheet2").Cells(x + 1, 2) = a(x)

Sheets("sheet3").Cells(1, x + 1) = b(x)
Sheets("sheet3").Cells(2, x + 1) = a(x)


Next x
End Sub

不重复字段的数字累加,然后放到行或列.rar

1.83 KB, 下载次数: 19

发表于 2011-10-4 10:30 | 显示全部楼层
回复 zzk386 的帖子

看不懂原始数据
最好楼主提供下结果数据是什么样子的。
回复

使用道具 举报

发表于 2011-10-4 11:09 | 显示全部楼层
回复 zzk386 的帖子
  1. Sub 按钮1_Click()
  2. Dim arr, obj_d As Object, x&
  3. Set obj_d = CreateObject("scripting.dictionary")
  4. With Sheet1
  5. arr = .Range("A2:B" & .Range("A65536").End(xlUp).Row)
  6. End With
  7. For x = 1 To UBound(arr)
  8. If arr(x, 2) <> "" Then
  9. obj_d(arr(x, 2)) = obj_d(arr(x, 2)) + arr(x, 1)
  10. End If
  11. Next x
  12. With Sheet2
  13. .Range("A2:B65536").ClearContents
  14. .Range("A2").Resize(obj_d.Count, 1) = Application.Transpose(obj_d.keys)
  15. .Range("B2").Resize(obj_d.Count, 1) = Application.Transpose(obj_d.items)
  16. .Select
  17. End With
  18. End Sub
复制代码
不重复字段的数字累加,然后放到行或列.zip (10.99 KB, 下载次数: 12)
回复

使用道具 举报

发表于 2011-10-4 11:42 | 显示全部楼层    本楼为最佳答案   
Sub aa()
Dim x As Integer
Dim d As Object
Dim a
Dim b

Set d = CreateObject("Scripting.Dictionary")
For x = 1 To Range("A65536").End(xlUp).Row
    If Cells(x, 2) <> "" Then
        d(Cells(x, 2).Value) = Cells(x, 1).Value + d(Cells(x, 2).Value)
    End If
Next x
a = d.Items
b = d.keys
For x = 0 To d.Count - 1
Sheets("sheet2").Cells(x + 1, 1) = b(x)
Sheets("sheet2").Cells(x + 1, 2) = a(x)

Sheets("sheet3").Cells(1, x + 1) = b(x)
Sheets("sheet3").Cells(2, x + 1) = a(x)


Next x
End Sub
回复

使用道具 举报

发表于 2011-10-4 11:44 | 显示全部楼层
放入sheet1代码窗口
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-28 04:29 , Processed in 0.252398 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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