我有一个生成两列数据的VBA代码。重复此分析以在相同列中生成更多数据,但针对不同条件,并将此数据置于第一次迭代的输出之下。如何获取生成XY Scatter的代码,在每个生成的数据块旁边都有平滑的线条?我的尝试在下面但是我遇到了三个问题:1)它只在调试停止宏之前生成一个图形,2)它生成条形图而不是散点图3)如何生成每个生成的图形以进行缩放放置它正在绘制的数据旁边,以便我得到一列图表?
Sub TestExample()
Dim NoIteration As Integer
Dim Iteration As Integer
NoIteration = Range("N26").Value
Dim YieldIteration As Integer
Dim NoOfJumps As Long
Dim minyield As Long
Dim maxyield As Long
Dim jump As Long
NoOfJumps = Range("Q24").Value
minyield = Range("Q25").Value
maxyield = Range("Q26").Value
jump = Range("Q27").Value
Dim xaxis As Range
Dim yaxis As Range
Dim c As Chart
Dim Sh As String
Sh = ActiveSheet.Name
Range("M29:T1000").Select
Selection.Clear
For jump = 0 To NoOfJumps
For Iteration = 0 To NoIteration
'Print Intervals
Range("M30").Offset(NoIteration + Iteration + 4, 0).Value = Range("V19").Value * Iteration
'Solve weights for minimum Spot SD for each given interval
SolverReset
SolverOk SetCell:="$T$18", MaxMinVal:=2, ValueOf:="0", ByChange:="$O$20:$R$20"
SolverAdd CellRef:="$T$17", Relation:=2, FormulaText:=Range("M30").Offset(NoIteration + Iteration + 4, 0).Value
SolverAdd CellRef:="$T$20", Relation:=2, FormulaText:=1
SolverAdd CellRef:="$T$7", Relation:=1, FormulaText:=minyield + jump 'State min required yield
SolverAdd CellRef:="$T$7", Relation:=3, FormulaText:=maxyield + jump 'State max required yield
SolverAdd CellRef:="$O$20:$R$20", Relation:=3, FormulaText:="0"
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
'Print Income Return, SD
Range("N30").Offset(Iteration + jump * 50, 0).Value = Range("T7").Value
Range("O30").Offset(Iteration + jump * 50, 0).Value = Range("T8").Value
'Print Spot Return, SD
Range("N30").Offset(NoIteration + Iteration + 4 + jump * 50, 0).Value = Range("T17").Value
Range("O30").Offset(NoIteration + Iteration + 4 + jump * 50, 0).Value = Range("T18").Value
'Print Total Return, SD
Range("N30").Offset(NoIteration * 2 + Iteration + 8 + jump * 50, 0).Value = Range("AC17").Value
Range("O30").Offset(NoIteration * 2 + Iteration + 8 + jump * 50, 0).Value = Range("AC18").Value
Next Iteration
Set yaxis = Range(Range("N30").Offset(Iteration + jump * 50, 0), Range("N30").End(xlDown))
Set xaxis = Range(Range("O30").Offset(Iteration + jump * 50, 0), Range("$O30").End(xlDown))
Set c = ActiveWorkbook.Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=Sh
'Set c = c.Location(Where:=xlLocationAsObject, Name:="Sheet1")
With c
.ChartType = xlXYScatterLines
' set other chart properties
End With
Dim s As Series
Set s = c.SeriesCollection.NewSeries
With s
.Values = yaxis
.XValues = xaxis
End With
Next jump
End Sub
答案 0 :(得分:1)
此代码查找列中的代码并绘制指定范围内的图形。当第一个代码通过循环时,它会偏移查找和绘图。
Dim srs As Series
Set cht = ActiveChart
'GRAPH 1
Set rng1 = ActiveSheet.Range(Range("W30").Offset(jump * 50, 0), Range("AA40").Offset(jump * 50, 0))
With ActiveSheet.ChartObjects.Add(Left:=rng1.Left, Width:=rng1.Width, Top:=rng1.Top, Height:=rng1.Height)
'(Left:=100, Width:=375, Top:=75, Height:=225)
.Chart.ChartType = xlXYScatterLines
Set srs = .Chart.SeriesCollection.NewSeries
srs.Name = "Graph1" '
srs.XValues = Range(Range("O30").Offset(jump * 50, 0), Range("O30").Offset(jump * 50, 0).End(xlDown))
srs.Values = Range(Range("N30").Offset(jump * 50, 0), Range("N30").Offset(jump * 50, 0).End(xlDown))
End With
End With
答案 1 :(得分:0)
当我们没有您正在使用的数据时,很多问题很难分析。但是,请参阅下一个可能有用并解决第一个问题的要点。
如果您最终将图表ChartObject
放在某张表上,则可以采用不同的方式。此外,默认情况下,新图表为条形样式。为了避免将其更改为ScatterLines(我猜这会导致一些例外)尝试使用以下代码(而不是在某些区域中):
Dim c As Chart
==>更改为Dim c as Shape
整个部分以:Set c=Activeworkbook.Charts.Add
开头,直到End With
替换为以下代码:
Set c = Sheets(Sh).Shapes.AddChart(xlXYScatterLines, 10, 10, 200, 200)
Dim s As Series
Set s = c.Chart.SeriesCollection.NewSeries
With s
.Values = yaxis
.XValues = xaxis
End With
要设置图表的位置,请尝试在循环结束前使用类似的解决方案:
'setting position
With c
.Left = Range("M10").Offset(0,1).Left
.Top = Range("M10").Offset(0,1).Top
End With
但不是Range("M10")
而是放置数据范围的引用。