调用使用 ARRAY 数据生成图表的 VBA 过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16230319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:51:01  来源:igfitidea点击:

Calling on a VBA procedure that generates a chart using ARRAY data

arraysvbacharts

提问by seigna

So, I have created an array, and I am trying to put the data into a chart... but I have no clue how to make the chart take on the data from an array I generated, rather than from a range. I coded for a chart uding a Range from an excel sheet.. but I can't see how to make all of that happen when I have this array..

所以,我创建了一个数组,我试图将数据放入图表中......但我不知道如何使图表从我生成的数组中获取数据,而不是从一个范围中获取数据。我使用 Excel 表格中的 Range 为图表编码..

I have two sub procedures. Inside one of them I want to use

我有两个子程序。在其中之一我想使用

Call ChartNew2(myArray)

How can I do that? I tried in this way and I wasn't successful either...

我怎样才能做到这一点?我也是这样试的,也没成功。。。

Sub ChartNew2(result2 As Variant)
Dim i As Integer
ReDim result2(1 To 4, 1 To 1)
Charts.Add
    For i = LBound(result2, 1) To UBound(result2, 1)
        result2(i, 1) = result2
    Next i
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.Location Where:=xlLocationAsObject

    With ActiveChart
                .HasTitle = True
                .Axes(xlValue, xlPrimary).HasTitle = True
        End With


    With ActiveChart.Axes(xlValue)
                .HasMajorGridlines = True
    End With

    ActiveChart.HasLegend = False
    ActiveChart.PlotArea.Select

    Selection.Interior.ColorIndex = xlAutomatic
    ActiveChart.Axes(xlValue).Select

    With ActiveChart.Axes(xlValue)

            .MaximumScale = 1

    End With

End Sub

回答by Nick.McDermaid

This link: http://www.excelforum.com/excel-charting-and-pivots/400227-making-charts-from-arrays-in-vba.html

此链接:http: //www.excelforum.com/excel-charting-and-pivots/400227-making-charts-from-arrays-in-vba.html

says to use this syntax:

说使用这个语法:

With ActiveChart.SeriesCollection(1)
    .XValues = MyXArray
    .Values = MyYArray
    .Name = MyName
End With

If you're having troubles passing arrays, I think this is the syntax if your array is an integer array.

如果您在传递数组时遇到麻烦,我认为如果您的数组是整数数组,这就是语法。

Sub ChartNew2(result2() As Integer)