0

我有以下宏,它为三列绘制散点图。一列(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
4

2 回答 2

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
于 2013-04-19T16:31:41.183 回答
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
于 2013-04-15T13:44:57.863 回答