我有一个问题,是否可以在For-To循环功能中创建图表。我有以下代码进行计算;
sub calculate_data()
dim npoint as double()
dim ncumulative as double()
dim nfactor as double()
dim npointfactor() as double
dim lastrowdata as long
dim n as long
lastrowdata = sheets("Sheet1").Range("A2").end(xldown).row -2
Redim npoint(lastrowdata)
Redim ncumulative(lastrowdata)
Redim nfactor(lastrowdata)
Redim npointfactor(lastrowdata)
For n = 1 To lastrowdata
npoint(n) = Sheets("Sheet1").Cells(2 + n, 2)
ncumulative(n) = Sheets("Sheet1").Cells(2 + n, 3)
nfactor(n) = Sheets("Sheet1").Cells(2 + n, 4)
npointfactor(n) = npoint(n) / nfactor(n)
next n
for n = 1 To lastrowdata
Sheets("Sheet1").cells(2 + n, 5) = npointfactor(n)
next n
end sub
然后,我希望通过将npointfactor(n)绘制为y轴并将ncumulative(n)绘制为x轴来绘制图表。有没有办法得到这个?
答案 0 :(得分:0)
而不是循环,只需在工作表中放置一个公式。然后制作你的图表:
Sub Calc_Data_2()
Dim sht1 As Worksheet
Dim LastRowData As Long
Dim rChartX As Range, rChartY As Range
Set sht1 = Sheets("Sheet1") ' or ActiveSheet
LastRowData = sht1.Range("A2").End(xlDown).Row - 2
Set rChartX = sht1.Range("C3").Resize(LastRowData)
Set rChartY = sht1.Range("E3").Resize(LastRowData)
rChartY.Formula = "=B3/D3"
'With sht1.Shapes.AddChart.Chart ' Excel 2007-2010
With sht1.Shapes.AddChart2.Chart ' Excel 2013-2016
.ChartType = xlXYScatterLines
.SetSourceData Union(rChartX, rChartY)
End With
End Sub