系列VBA Scatter Graph之间的颜色不同

时间:2013-04-15 13:09:20

标签: excel vba graph

我有以下宏,它绘制了三列的Scatter图。一列(AL13,向下)位于x轴上。如何将其他两列(AK和AM)绘制到同一个散射图上?还有彼此不同的颜色?谢谢

Sub Graphing()

        Set rng4 = ActiveSheet.Range(Range("AP13"), Range("AV33"))

With ActiveSheet.ChartObjects.Add(Left:=rng4.Left, Width:=rng4.Width, Top:=rng4.Top, Height:=rng4.Height)
    .Chart.ChartType = xlXYScatter
    .Chart.HasLegend = False
    .Chart.Axes(xlCategory).TickLabels.Font.Size = 18
    .Chart.Axes(xlValue).TickLabels.Font.Size = 18
    Set srs = .Chart.SeriesCollection.NewSeries
    srs.Values = Range(Range("AK13"), Range("AK13").End(xlDown))
    srs.XValues = Range(Range("AL13"), Range("AL13").End(xlDown))
    srs.Values = Range(Range("AM13"), Range("AM13").End(xlDown)) 

End With
End Sub

2 个答案:

答案 0 :(得分:1)

我发现如果我将系列设置为两个单独的系列,那么它将绘制两个并给它们不同的颜色。不确定它是否是最有效的方法,但它有效。

    Sub Graphing()
    'Declare all the variables to be used:'
    Dim rng4 as Range
    Dim srs as Series
    Dim cht as Chart
    Dim xVals as Range
    Dim srsVals as Range 

    'Set the chart's data range:'
    Set rng4 = ActiveSheet.Range(Range("AP13"), Range("AV33"))

    'Set the range variable to contain the series values'
    ' You can later modify this to include any number of columns, and the '
    '  loop structure below will add each column as a series to the chart.'
    Set srsVals = ActiveSheet.Range(Range("AL13"),Range("AM13").End(xlDown))

    'Set the cht variable:'
    Set cht= ActiveSheet.ChartObjects.Add(Left:=rng4.Left, Width:=rng4.Width, Top:=rng4.Top, Height:=rng4.Height).Chart

    'Set the Range variable for xValues:
    Set xVals = Range(Range("AK13"),Range("AK13").End(xlDown))

    'Format the chart and add series to the chart by iterating over the columns in srsVals:'
    With cht
        .ChartType = xlXYScatter
        .HasLegend = False
        .Axes(xlCategory).TickLabels.Font.Size = 18
        .Axes(xlValue).TickLabels.Font.Size = 18

        'Create the series in a loop
        For c = 1 to srsVal.Columns.Count
            Set srs = .SeriesCollection.NewSeries
            With srs
                .Values = xVals
                .XValues = Range(srsVals.Columns(c).Address)
                .Name = "Series " & c '<-- Modify as needed.'
            End With
        Next

    End With
End Sub

答案 1 :(得分:1)

我将重新发布我上面为您修改过的代码,感谢您对我的信任:)

Sub Graphing()
'Declare all the variables to be used:'
Dim rng4 as Range
Dim srs as Series
Dim cht as Chart
Dim xVals as Range
Dim srsVals as Range 

'Set the chart's data range:'
Set rng4 = ActiveSheet.Range(Range("AP13"), Range("AV33"))

'Set the range variable to contain the series values'
' You can later modify this to include any number of columns, and the '
'  loop structure below will add each column as a series to the chart.'
Set srsVals = ActiveSheet.Range(Range("AL13"),Range("AM13").End(xlDown))

'Set the cht variable:'
Set cht= ActiveSheet.ChartObjects.Add(Left:=rng4.Left, Width:=rng4.Width, Top:=rng4.Top, Height:=rng4.Height).Chart

'Set the Range variable for xValues:
Set xVals = Range(Range("AK13"),Range("AK13").End(xlDown))

'Format the chart and add series to the chart by iterating over the columns in srsVals:'
With cht
    .ChartType = xlXYScatter
    .HasLegend = False
    .Axes(xlCategory).TickLabels.Font.Size = 18
    .Axes(xlValue).TickLabels.Font.Size = 18

    'Create the series in a loop
    For c = 1 to srsVal.Columns.Count
        Set srs = .SeriesCollection.NewSeries
        With srs
            .Values = xVals
            .XValues = Range(srsVals.Columns(c).Address)
            .Name = "Series " & c '<-- Modify as needed.'
        End With
    Next

End With
End Sub