Excel精英培训网

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

[已解决]请求帮忙修改代码

[复制链接]
发表于 2013-9-27 11:42 | 显示全部楼层 |阅读模式
本帖最后由 douya33 于 2013-9-27 13:12 编辑

附件文件运行下段代码时出现“运行时错误13,类型不匹配”错误,请求各位大侠谁能帮我改改代码啊?红色为出错部分。
补充说明一下:是有的时候会出错,并不是每次都会出错。
Sub test()
Application.ScreenUpdating = False
Dim arr, d, k, t, s, brr
Set d = CreateObject("Scripting.Dictionary")
arr = Range("a1").CurrentRegion
[g2:j999].ClearContents
For i = 2 To UBound(arr)
s = arr(i, 4) & "|" & arr(i, 5)
If d.exists(s) Then
d(s) = d(s) & " " & arr(i, 2)
Else
d(s) = arr(i, 2)
End If
Next
k = d.keys
t = d.items
[i2].Resize(d.Count) = Application.Transpose(k)
[g2].Resize(d.Count) = Application.Transpose(t)
d.RemoveAll
brr = Range("i2").CurrentRegion
For i = 2 To UBound(arr)
s = arr(i, 4) & "|" & arr(i, 5)
d(s) = d(s) + arr(i, 3)
Next
[h2].Resize(d.Count) = Application.Transpose(d.items)
Application.DisplayAlerts = False
[i2].Resize(d.Count).TextToColumns Other:=True, OtherChar:="|"
Application.DisplayAlerts = True
Set d = Nothing
Range("g2").CurrentRegion.Sort [i2], 1
With Range("g2").CurrentRegion
.Borders.LineStyle = xlContinuous
.WrapText = True
.Rows.AutoFit
End With
Application.ScreenUpdating = True
End Sub
自动合并.rar (36.69 KB, 下载次数: 11)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-9-27 11:48 | 显示全部楼层
      Range("d1").Resize(d.Count) = Application.Transpose(d.Keys)
      Range("e1").Resize(d.Count) = Application.Transpose(d.Items)
直接这样不行么
回复

使用道具 举报

 楼主| 发表于 2013-9-27 12:11 | 显示全部楼层
不行哦,
Range("e1").Resize(d.Count) = Application.Transpose(d.Items)
这句还提示错误
回复

使用道具 举报

发表于 2013-9-27 13:00 | 显示全部楼层
本帖最后由 danio112 于 2013-9-27 13:10 编辑

换成这个试试 [g2].Resize(d.Count) = d.items
回复

使用道具 举报

 楼主| 发表于 2013-9-27 13:09 | 显示全部楼层
danio112 发表于 2013-9-27 13:00
换成这个试试 [g2].Resize(d.Count) = t

还有一点问题,B列的数据不能自动合并。如下图:
执行前:
R501 R502 R804 R805
4
01.02.01.0009A
YAGEO
R912
1
01.02.01.0009A
YAGEO
执行后:
R726
5
01.02.01.0009A
YAGEO
正确的结果应该是:
R501 R502 R804 R805 R912
5
01.02.01.0009A
YAGEO
回复

使用道具 举报

发表于 2013-9-27 13:33 | 显示全部楼层
douya33 发表于 2013-9-27 12:11
不行哦,
Range("e1").Resize(d.Count) = Application.Transpose(d.Items)
这句还提示错误

出错原因是Application.Transpose(),括号的内容字符不能超过256个
回复

使用道具 举报

发表于 2013-9-27 13:41 | 显示全部楼层
加了一句就好了,不过只能保证本附件数据才可以.
其实主要原因是因为:item中字符串长度超过255个了吧!,个人认为是这样的.
  1. d("01.02.01.0035A|YAGEO") = ""
  2. k = d.keys
  3. t = d.items
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-9-27 13:51 | 显示全部楼层
sliang28 发表于 2013-9-27 13:41
加了一句就好了,不过只能保证本附件数据才可以.
其实主要原因是因为:item中字符串长度超过255个了吧!,个人 ...

运行时错误‘1004’,未选定要分列的数据.{:041:}
回复

使用道具 举报

发表于 2013-9-27 13:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 美斯特邦威 于 2013-9-27 13:56 编辑
douya33 发表于 2013-9-27 13:51
运行时错误‘1004’,未选定要分列的数据.
  1. Sub test()
  2. Application.ScreenUpdating = False
  3. Dim pp(1 To 100000, 1 To 3)
  4. Dim arr, d, k, t, s, brr
  5. Set d = CreateObject("Scripting.Dictionary")
  6. arr = Range("a1").CurrentRegion
  7. [g2:j999].ClearContents
  8. For i = 2 To UBound(arr)
  9. s = arr(i, 4) & "|" & arr(i, 5)
  10. If d.exists(s) Then
  11. pp(d(s), 1) = pp(d(s), 1) & " " & arr(i, 2)
  12. pp(d(s), 2) = pp(d(s), 2) + arr(i, 3)
  13. Else
  14. d(s) = x + 1
  15. pp(x + 1, 3) = s
  16. pp(x + 1, 1) = arr(i, 2)
  17. pp(x + 1, 2) = arr(i, 3)
  18. x = x + 1
  19. End If
  20. Next i
  21. Range("g2").Resize(x, 3) = pp
  22. Application.DisplayAlerts = False
  23. [i2].Resize(d.Count).TextToColumns Other:=True, OtherChar:="|"
  24. Application.DisplayAlerts = True
  25. Set d = Nothing
  26. Range("g2").CurrentRegion.Sort [i2], 1
  27. With Range("g2").CurrentRegion
  28. .Borders.LineStyle = xlContinuous
  29. .WrapText = True
  30. .Rows.AutoFit
  31. End With
  32. Application.ScreenUpdating = True
  33. End Sub
复制代码
前半部分给你换为数组
OK了,看看如何

回复

使用道具 举报

发表于 2013-9-27 14:02 | 显示全部楼层
douya33 发表于 2013-9-27 13:09
还有一点问题,B列的数据不能自动合并。如下图:
执行前:
执行后:

代码其实真的没有错,我运行的时候成功了,可过了一会儿,又不行了,甚是奇怪,于是只好换一种输出方式输出了。
For j = 0 To d.Count - 1
Cells(j + 2, "i") = k(j)
Cells(j + 2, "g") = t(j)
Next j
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 15:36 , Processed in 0.764781 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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