vba中的424代码和graphics创build

我对VBA相对来说比较陌生 – 即使我不了解它,我也倾向于使用那些有用的东西。

我试图为工作表中的每一行数据生成单独的散点图。 我很好configuration图表等,但要读取每一行,并正确迭代超出了我!

这是我目前所拥有的:

Private Sub CommandButton1_Click() ActiveWorkbook.Charts.Add Dim i As Integer For i = 2 To WS.Range("A65536").End(xlUp).Row With ActiveWorkbook.ActiveChart 'Data? .ChartType = xlXYScatter .SeriesCollection.NewSeries .SeriesCollection(1).Name = "Progress" .SeriesCollection(1).XValues = "=Sheet2!$B$1:$J$1" .SeriesCollection(1).Values = "=Sheet2!$B$" & i & ":$J$" & i 'Titles .HasTitle = True .ChartTitle.Characters.Text = "valuefromcellN2?" .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Timeline" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Grade" .Axes(xlCategory).HasMajorGridlines = True 'Formatting .Axes(xlCategory).HasMinorGridlines = False .Axes(xlValue).HasMajorGridlines = True .Axes(xlValue).HasMinorGridlines = False .HasLegend = False End With Next End Sub 

当我运行这个,我得到一个运行时错误424 – 所需的对象。 我的问题是:

  1. 我是否以正确的方式去解决这个问题?
  2. 我错过了什么导致此运行时错误?
  3. 我如何从cellN2获得价值? 实际显示的价值,而不是文字?

谢谢你的帮助!

山姆

编辑:

感谢您的帮助 – 我认为我现在已经正确地更新了它(它适用于所有标题并创build新graphics!)

 Private Sub CommandButton1_Click() ActiveWorkbook.Charts.Add Dim ws As Worksheet Set ws = tracker Dim i As Long For i = 2 To ws.Range("A65536").End(xlUp).Row Charts.Add With ActiveWorkbook.ActiveChart 'Data? .ChartType = xlXYScatter .SeriesCollection.NewSeries .SeriesCollection(1).Name = "Progress" .SeriesCollection(1).XValues = "=tracker!$B$1:$J$1" .SeriesCollection(1).Values = "=tracker!$B$" & i & ":$J$" & i 'Titles .HasTitle = True .ChartTitle.Characters.Text = Range("N" & i).Value .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Timeline" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Grade" .Axes(xlCategory).HasMajorGridlines = True 'Formatting .Axes(xlCategory).HasMinorGridlines = False .Axes(xlValue).HasMajorGridlines = True .Axes(xlValue).HasMinorGridlines = False .HasLegend = False End With Next End Sub 

Q1我是否以正确的方式处理这个问题?

是的,你是。 你只需要解决一些问题。

Q2我错过了什么导致这个运行时错误?

你正在得到这个错误,因为你还没有声明ws对象。 我稍后在下面的代码中解释了如何去做。

Q3如何从cellN2获取价值? 实际显示的价值,而不是文字?

为了得到一个特定的单元格的值,你可以说Range("A1").Value在你的情况下,它的Range("A1").ValueRange("N2").Value

其他几件事情

  1. 使用Excel行时,请避免使用Integer 。 使用Long 。 发布Excel 2007后,行数增加, Integer可能无法容纳更大的值。

  2. 避免使用像65536这样的硬编码值。 请参阅如何查找最后一行。

我已经评论了代码,所以你不应该有理解它的问题。 但是,如果你这样做,然后只是问。

这是你正在尝试( 未经testing )?

 Private Sub CommandButton1_Click() Dim ws As Worksheet Dim i As Long, LRow As Long Dim chrt As Chart '~~> This is where we will have the chart '~~> We declare the object here. Change as applicable Set ws = ThisWorkbook.Sheets("Sheet2") With ws '~~. Find the last row in Col A LRow = .Range("A" & .Rows.Count).End(xlUp).Row '~~> Add Chart Set chrt = .Shapes.AddChart.Chart With chrt '<~~ Work with chart and set it's parameters .ChartType = xlXYScatter .HasTitle = True .ChartTitle.Characters.Text = ws.Range("N2").Value '<~~ Set the Value here .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Timeline" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Grade" .Axes(xlCategory).HasMajorGridlines = True 'Formatting .Axes(xlCategory).HasMinorGridlines = False .Axes(xlValue).HasMajorGridlines = True .Axes(xlValue).HasMinorGridlines = False .HasLegend = False n = 1 '~~> Add series and set it For i = 2 To LRow .SeriesCollection.NewSeries .SeriesCollection(n).Name = "Progress" .SeriesCollection(n).XValues = "=Sheet2!$B$1:$J$1" .SeriesCollection(n).Values = "=Sheet2!$B$" & i & ":$J$" & i n = n + 1 Next i End With End With End Sub