0

我有一个生成两列数据的 VBA 代码。重复此分析以在相同列中生成更多数据,但条件不同,并将此数据放在第一次迭代的输出下方。如何获取代码以在生成的每个数据块旁边生成带有平滑线的 XY 散点图?我的尝试如下,但是我遇到了三个问题: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
4

2 回答 2

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
于 2013-04-03T09:17:45.337 回答
0
  1. 当我们没有您正在使用的数据时,可能会有很多难以分析的问题。但是,请参阅可能有帮助的下一点并解决第一个问题。

  2. 如果您最终将图表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
  1. 要设置图表的位置,请尝试在循环结束之前使用类似的解决方案:

    'setting position
    With c
        .Left = Range("M10").Offset(0,1).Left
        .Top = Range("M10").Offset(0,1).Top
    End With
    

但不是Range("M10")把你的数据范围的参考。

于 2013-04-03T07:36:42.297 回答