如何使用vba不花时间生成图形?

时间:2012-10-17 12:31:15

标签: excel excel-vba vba

您好我已经编写了一个生成图表的代码,但它运行正常。 问题是它需要花费大量时间来生成。我不知道为什么需要时间。 代码是

    Dim cc As Chart

    Set cc = ActiveWorkbook.Charts.Add
    Set cc = cc.Location(Where:=xlLocationAsObject, name:=assume)

    With cc

     .ChartType = xlXYScatterLines

     With .Parent

       .Top = Columns(b).Offset(0, 4).Top
       .Left = Columns(b).Offset(0, 4).Left
       .name = "cc"

     End With

  End With

  Dim sc As Series
  Set sc = cc.SeriesCollection(1)

  With sc
      .Values = Columns(b).Offset(0, -3)
      .XValues = Columns(b).Offset(0, -5)
  End With

请有人帮助我

2 个答案:

答案 0 :(得分:1)

您是否关闭了屏幕更新?将其添加到代码的开头:

Application.ScreenUpdating = False

然后在代码的最后添加:

Application.ScreenUpdating = True

答案 1 :(得分:1)

试试这个,只用实际数据绘制列的行。

Sub makeChart(b As String, assume As String) 'i presupposed these arguments based on your code

Application.ScreenUpdating = False

Dim cc As Chart

Set cc = ActiveWorkbook.Charts.Add
Set cc = cc.Location(Where:=xlLocationAsObject, Name:=assume)

With cc

    .ChartType = xlXYScatterLines

    With .Parent

        .Top = Columns(b).Offset(0, 4).Top
        .Left = Columns(b).Offset(0, 4).Left
        .Name = "cc"

    End With

End With


Dim strValue As String, strXValue As String

'here you are using the passed column letter to find the specific column you want to chart
strValue = Split(Range(b & "1").Offset(, -3).Address, "$")(1) 'will return "C" if given column F
strXValue = Split(Range(b & "1").Offset(, -5).Address, "$")(1) 'will return "A" if given column F

Dim sc As Series
Set sc = cc.SeriesCollection(1)

With sc

    'will select from row 1 to the last true used row in the given column
    .Values = Range(strValue & "1:" & strValue & Range(strValue & Rows.Count).End(xlUp).Row)
    .XValues = Range(strXValue & "1:" & strXValue & Range(strXValue & Rows.Count).End(xlUp).Row)

End With

Application.ScreenUpdating = True

End Sub