Excel精英培训网

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

[已解决]VBA难题!!!请大侠解决,在线等~~~

[复制链接]
发表于 2013-12-11 17:27 | 显示全部楼层 |阅读模式
新建 Microsoft Office Excel 工作表 (3).rar (22.91 KB, 下载次数: 8)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-12-11 17:54 | 显示全部楼层
错误1:
arr = Range("a2:m50" & [a65536].End(xlUp).Row)
这里写错了,应该写成:
arr = Range("a2:m" & [a65536].End(xlUp).Row)

回复

使用道具 举报

发表于 2013-12-11 18:16 | 显示全部楼层
错了,还有个前缀没有加。
arr = sheet1.Range("a2:m" & sheet1.[a65536].End(xlUp).Row)
回复

使用道具 举报

发表于 2013-12-11 19:03 | 显示全部楼层
  1. Sub teset2()
  2.     Dim arr, temp()
  3.     Dim arr2, lRow As Long, lCol As Long
  4.     Dim dicName As Object
  5.     Dim dic As Object
  6.     Dim i As Long

  7.     Set dic = CreateObject("scripting.dictionary")
  8.     Set dicName = CreateObject("scripting.dictionary")
  9.     arr = Sheets("sheet1").Range("a6:m" & Sheet1.[a65536].End(xlUp).Row)
  10.     For i = 1 To UBound(arr, 1)

  11.         If Not dicName.exists(arr(i, 2)) Then
  12.             '数组扩容,3列一组
  13.             ReDim Preserve temp(1 To UBound(arr), 1 To (dicName.Count + 1) * 3)
  14.             dicName.Add arr(i, 2), Array(dicName.Count + 1, 0)
  15.             '列号:数量
  16.         End If

  17.         lCol = (dicName(arr(i, 2))(0)) * 3 - 2

  18.         If Not dic.exists(arr(i, 2) & "#" & arr(i, 1)) Then
  19.             dic.Add arr(i, 2) & "#" & arr(i, 1), dicName(arr(i, 2))(1) + 1
  20.             '姓名#日期,行号
  21.             arr2 = dicName(arr(i, 2))
  22.             arr2(1) = arr2(1) + 1
  23.             dicName(arr(i, 2)) = arr2
  24.         End If

  25.         lRow = dicName(arr(i, 2))(1)
  26.         temp(lRow, lCol) = arr(i, 1)
  27.         temp(lRow, lCol + 1) = temp(lRow, lCol + 1) + arr(i, 12)
  28.         temp(lRow, lCol + 2) = temp(lRow, lCol + 2) + arr(i, 13)
  29.     Next
  30.     Application.ScreenUpdating = False
  31.    
  32.     ActiveSheet.UsedRange.ClearContents
  33.     Range("a4").Resize(UBound(arr), UBound(temp, 2)).Value = temp
  34.     ReDim temp(1 To 2, 1 To dicName.Count * 3)
  35.     arr2 = dicName.keys
  36.     For i = 1 To dicName.Count
  37.         temp(2, i * 3 - 2) = "日期"
  38.         temp(2, i * 3 - 1) = "销售额"
  39.         temp(2, i * 3) = "已收"
  40.     Next
  41.     lCol = 0
  42.     For i = 2 To dicName.Count * 3 Step 3
  43.         temp(1, i) = arr2(lCol)
  44.         lCol = lCol + 1
  45.     Next
  46.     Range("a2").Resize(UBound(temp), UBound(temp, 2)).Value = temp
  47.     Application.ScreenUpdating = True
  48.     Set dicName = Nothing
  49.     Set dic = Nothing
  50.     MsgBox "统计完成"
  51. End Sub
复制代码
用的字典装数组的方法,复杂了,如果同一个人没有日期重复的记录,代码可以更少。

回复

使用道具 举报

发表于 2013-12-11 19:30 | 显示全部楼层    本楼为最佳答案   
  1. Sub teset22()
  2.     Dim arr, temp()
  3.     Dim lRow As Long, lCol As Long
  4.     Dim dicName As Object, dic As Object, icRow As Object
  5.     Dim i As Long

  6.     Set dic = CreateObject("scripting.dictionary")
  7.     Set dicName = CreateObject("scripting.dictionary")
  8.     Set dicRow = CreateObject("scripting.dictionary")

  9.     arr = Sheets("sheet1").Range("a6:m" & Sheet1.[a65536].End(xlUp).Row)
  10.     For i = 1 To UBound(arr, 1)

  11.         If Not dicName.exists(arr(i, 2)) Then
  12.             '数组扩容,3列一组
  13.             ReDim Preserve temp(1 To UBound(arr), 1 To (dicName.Count + 1) * 3)
  14.             dicName.Add arr(i, 2), dicName.Count + 1
  15.             '列号
  16.             dicRow.Add arr(i, 2), 1
  17.         End If

  18.         '对应姓名在哪一列
  19.         lCol = dicName(arr(i, 2)) * 3 - 2
  20.         '行号
  21.         lRow = dicRow(arr(i, 2))
  22.         temp(lRow, lCol) = arr(i, 1)
  23.         temp(lRow, lCol + 1) = arr(i, 12)
  24.         temp(lRow, lCol + 2) = arr(i, 13)
  25.         dicRow(arr(i, 2)) = dicRow(arr(i, 2)) + 1
  26.     Next
  27.    
  28.     Application.ScreenUpdating = False
  29.    
  30.     ActiveSheet.UsedRange.Offset(2).ClearContents
  31.     Range("a4").Resize(UBound(arr), UBound(temp, 2)).Value = temp
  32.     ReDim temp(1 To dicName.Count * 3)
  33.     arr = dicName.keys
  34.     For i = 0 To UBound(arr)
  35.         temp(i * 3 + 2) = arr(i)
  36.     Next
  37.     Range("a2").Resize(, UBound(temp)).Value = temp
  38.    
  39.     Application.ScreenUpdating = True
  40.     Set dicName = Nothing
  41.     Set dic = Nothing
  42.     Set dicRow = Nothing
  43.    
  44.     MsgBox "统计完成"
  45. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-12-11 19:34 | 显示全部楼层
hwc2ycy 发表于 2013-12-11 19:03
用的字典装数组的方法,复杂了,如果同一个人没有日期重复的记录,代码可以更少。

大神膜拜!!太谢谢您了,厉害
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 14:41 , Processed in 0.269650 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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