Excel精英培训网

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

[已解决]怎么给代码加上循环?

[复制链接]
发表于 2013-4-29 10:50 | 显示全部楼层 |阅读模式
本帖最后由 jzzhmm425 于 2013-4-29 11:06 编辑

下面这段代码是给工作表中插入图表的一段代码,但是我加上循环后就无法运行了?谁能帮我加上循环,让每张工作表都能运行这段代码。
  1. Sub ChartAdd()
  2. Dim myRange As Range
  3. Dim myChart As ChartObject
  4. Dim R As Integer
  5. With Sheet1
  6. .ChartObjects.Delete
  7. R = .Range("A65536").End(xlUp).Row
  8. Set myRange = .Range("A" & 1 & ":B" & R)
  9. Set myChart = .ChartObjects.Add(120, 40, 400, 250)
  10. With myChart.Chart
  11. .ChartType = xlColumnClustered
  12. .SetSourceData Source:=myRange, PlotBy:=xlColumns
  13. .ApplyDataLabels ShowValue:=True
  14. .HasTitle = True
  15. .ChartTitle.Text = "图表制作示例"
  16. With .ChartTitle.Font
  17. .Size = 20
  18. .ColorIndex = 3
  19. .Name = "华文新魏"
  20. End With
  21. With .ChartArea.Interior
  22. .ColorIndex = 8
  23. .PatternColorIndex = 1
  24. .Pattern = xlSolid
  25. End With
  26. With .PlotArea.Interior
  27. .ColorIndex = 35
  28. .PatternColorIndex = 1
  29. .Pattern = xlSolid
  30. End With
  31. .SeriesCollection(1).DataLabels.Delete
  32. With .SeriesCollection(2).DataLabels.Font
  33. .Size = 10
  34. .ColorIndex = 5
  35. End With
  36. End With
  37. End With
  38. Set myRange = Nothing
  39. Set myChart = Nothing
  40. End Sub
复制代码
我把这段代码中的
With Sheet1
换成
For x = 2 To Sheets.Count
        With Sheets(x)
变成下面这段代码后,总提示说“ApplyDataLabels用作chart对象失败”,不知道是哪里错了。
  1. Sub ChartAdd()
  2. Dim myRange As Range
  3. Dim myChart As ChartObject
  4. Dim R As Integer
  5. For x = 3 To Sheets.Count
  6. With Sheets(x)
  7. .ChartObjects.Delete
  8. R = .Range("A65536").End(xlUp).Row
  9. Set myRange = .Range("A" & 1 & ":B" & R)
  10. Set myChart = .ChartObjects.Add(120, 40, 400, 250)
  11. With myChart.Chart
  12. .ChartType = xlColumnClustered
  13. .SetSourceData Source:=myRange, PlotBy:=xlColumns
  14. .ApplyDataLabels ShowValue:=True
  15. .HasTitle = True
  16. .ChartTitle.Text = "图表制作示例"
  17. With .ChartTitle.Font
  18. .Size = 20
  19. .ColorIndex = 3
  20. .Name = "华文新魏"
  21. End With
  22. With .ChartArea.Interior
  23. .ColorIndex = 8
  24. .PatternColorIndex = 1
  25. .Pattern = xlSolid
  26. End With
  27. With .PlotArea.Interior
  28. .ColorIndex = 35
  29. .PatternColorIndex = 1
  30. .Pattern = xlSolid
  31. End With
  32. .SeriesCollection(1).DataLabels.Delete
  33. With .SeriesCollection(2).DataLabels.Font
  34. .Size = 10
  35. .ColorIndex = 5
  36. End With
  37. End With
  38. End With
  39. Set myRange = Nothing
  40. Set myChart = Nothing
  41. Next
  42. End Sub
复制代码
最佳答案
2013-4-29 11:23
  1. Sub ChartAdd()
  2.     Dim myRange As Range
  3.     Dim myChart As ChartObject
  4.     Dim R As Integer
  5.     On Error Resume Next
  6.     For x = 1 To Sheets.Count
  7.         With Sheets(x)
  8.             Debug.Print .Name, .ChartObjects.Count
  9.             
  10.             .ChartObjects.Delete
  11.             R = .Range("A65536").End(xlUp).Row
  12.             Set myRange = .Range("A" & 1 & ":B" & R)
  13.             Set myChart = .ChartObjects.Add(120, 40, 400, 250)
  14.             With myChart.Chart
  15.                 .ChartType = xlColumnClustered
  16.                 .SetSourceData Source:=myRange, PlotBy:=xlColumns
  17.                 .ApplyDataLabels ShowValue:=True
  18.                 .HasTitle = True
  19.                 .ChartTitle.Text = "图表制作示例"
  20.                 With .ChartTitle.Font
  21.                     .Size = 20
  22.                     .ColorIndex = 3
  23.                     .Name = "华文新魏"
  24.                 End With
  25.                 With .ChartArea.Interior
  26.                     .ColorIndex = 8
  27.                     .PatternColorIndex = 1
  28.                     .Pattern = xlSolid
  29.                 End With
  30.                 With .PlotArea.Interior
  31.                     .ColorIndex = 35
  32.                     .PatternColorIndex = 1
  33.                     .Pattern = xlSolid
  34.                 End With
  35.                 .SeriesCollection(1).DataLabels.Delete
  36.                 With .SeriesCollection(2).DataLabels.Font
  37.                     .Size = 10
  38.                     .ColorIndex = 5
  39.                 End With
  40.             End With
  41.         End With
  42.         Set myRange = Nothing
  43.         Set myChart = Nothing
  44.     Next
  45. End Sub
复制代码

这是第二段代码运行后提示的错误

这是第二段代码运行后提示的错误

这是第一段代码运行后出来的表格

这是第一段代码运行后出来的表格

示例工作表.zip

7.66 KB, 下载次数: 3

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-4-29 11:00 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2013-4-29 11:06 | 显示全部楼层
hwc2ycy 发表于 2013-4-29 11:00
你传个附件吧。
要调试的话

好的,谢谢了                 
回复

使用道具 举报

发表于 2013-4-29 11:23 | 显示全部楼层    本楼为最佳答案   
  1. Sub ChartAdd()
  2.     Dim myRange As Range
  3.     Dim myChart As ChartObject
  4.     Dim R As Integer
  5.     On Error Resume Next
  6.     For x = 1 To Sheets.Count
  7.         With Sheets(x)
  8.             Debug.Print .Name, .ChartObjects.Count
  9.             
  10.             .ChartObjects.Delete
  11.             R = .Range("A65536").End(xlUp).Row
  12.             Set myRange = .Range("A" & 1 & ":B" & R)
  13.             Set myChart = .ChartObjects.Add(120, 40, 400, 250)
  14.             With myChart.Chart
  15.                 .ChartType = xlColumnClustered
  16.                 .SetSourceData Source:=myRange, PlotBy:=xlColumns
  17.                 .ApplyDataLabels ShowValue:=True
  18.                 .HasTitle = True
  19.                 .ChartTitle.Text = "图表制作示例"
  20.                 With .ChartTitle.Font
  21.                     .Size = 20
  22.                     .ColorIndex = 3
  23.                     .Name = "华文新魏"
  24.                 End With
  25.                 With .ChartArea.Interior
  26.                     .ColorIndex = 8
  27.                     .PatternColorIndex = 1
  28.                     .Pattern = xlSolid
  29.                 End With
  30.                 With .PlotArea.Interior
  31.                     .ColorIndex = 35
  32.                     .PatternColorIndex = 1
  33.                     .Pattern = xlSolid
  34.                 End With
  35.                 .SeriesCollection(1).DataLabels.Delete
  36.                 With .SeriesCollection(2).DataLabels.Font
  37.                     .Size = 10
  38.                     .ColorIndex = 5
  39.                 End With
  40.             End With
  41.         End With
  42.         Set myRange = Nothing
  43.         Set myChart = Nothing
  44.     Next
  45. End Sub
复制代码
回复

使用道具 举报

发表于 2013-4-29 11:23 | 显示全部楼层
QQ截图20130429112246.jpg
回复

使用道具 举报

发表于 2013-4-29 11:24 | 显示全部楼层
集合中没有任何对像的时候,你调用DELETE方法是会报错的

这里偷懒,直接用On Error Resume Next来过了。
回复

使用道具 举报

发表于 2013-4-29 11:28 | 显示全部楼层
另外,我跟你把循环改成了从1开始,shee2,sheet3因为表里没有数据,可以插入一个空图,但是后面进行数据标签的设置时就会报错。所以了用了on erorr resume 语句。偷懒时可以用,但是要写严谨了就不能这么操作了。
回复

使用道具 举报

 楼主| 发表于 2013-4-29 11:28 | 显示全部楼层
hwc2ycy 发表于 2013-4-29 11:23

能不能讲讲为啥要加上这两句代码呀
回复

使用道具 举报

发表于 2013-4-29 11:29 | 显示全部楼层
忽略错误,继续往下运行。
回复

使用道具 举报

 楼主| 发表于 2013-4-29 11:32 | 显示全部楼层
hwc2ycy 发表于 2013-4-29 11:29
忽略错误,继续往下运行。

嗯,这段代码还得学习一下了
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 04:41 , Processed in 0.329110 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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