如何将两个命名范围设置为图表数据源?

时间:2017-06-14 04:20:54

标签: vba

我有两个命名范围,详情如下:

Set JRange = sht.Range(sht.Cells(StartRow, 10), sht.Cells(LastRow, 10))
Set DRange = sht.Range(sht.Cells(StartRow, 4), sht.Cells(LastRow, 4))

我想将它们都用作指定图表宏的来源。这就是我所拥有的:

Set Chart = ActiveSheet.ChartObjects.Add(Left:=300, Width:=325.9842519685, Top:=10, Height:=277.5118110236)

With Chart
    .Chart.SetSourceData Source:=Range(DRange) And (JRange), PlotBy:=xlColumns
    .Chart.ChartType = xlLine
    .Chart.ChartStyle = 2
    .Height = 277.5118110236
    .Width = 325.9842519685

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以分别设置新系列“ValuesXValues

Sub Tester()

    Dim JRange As Range, DRange As Range, chtObj, cht As Chart
    Dim StartRow, LastRow, sht

    StartRow = 3 'for testing
    LastRow = 15 'for testing
    Set sht = ActiveSheet

    Set JRange = sht.Range(sht.Cells(StartRow, 10), sht.Cells(LastRow, 10))
    Set DRange = sht.Range(sht.Cells(StartRow, 4), sht.Cells(LastRow, 4))


    Set chtObj = ActiveSheet.ChartObjects.Add(Left:=300, _
               Width:=325.9842519685, Top:=10, Height:=277.5118110236)

    With chtObj.Chart
        .ChartType = xlLine
        .ChartStyle = 2
        With .SeriesCollection.NewSeries
            .Values = JRange
            .XValues = DRange
        End With
    End With

End Sub