动态更改图表系列VBA

时间:2017-08-02 08:21:24

标签: excel vba excel-vba

我正在尝试动态制作图表时间序列

为此,我将数据订单中的日期和利润从原始数据复制到另一个工作表,我将用它来动态创建图表。

但它给了我一个错误。在我创建范围的线附近,有更好的方法吗?

Sub ChartDataSeries()

    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim rng As Range

    Set ws = Sheets("Data - Orders")
    Set ws1 = Sheets("Sheet10")
    ws.Range("C:C").Copy 'Source Copy Order Date
    ws1.Activate
    ws1.Range("B1").Select 'Paste Order Date
    ActiveSheet.Paste

    ws.Range("I:I").Copy 'Copy Profit Series
    ws1.Activate
    ws1.Range("C1").Select
    ActiveSheet.Paste ' Paste Profit Series
    ws1.Range("B1").Select
    Set rng = Range(Selection, Selection.End(xlToRight)).Select ' Error Ocurring here
    ActiveSheet.ChartObjects("Chart 7").Activate
    ActiveChart.SetSourceData Source: rng.Select
    'ActiveChart.SeriesCollection(1).XValues = Range("Order Date")
    'ActiveChart.SeriesCollection(1).Values = Range("Profit")



End Sub

1 个答案:

答案 0 :(得分:2)

如果您远离所有这些ID+name 1 name1 2 name2 3 name3 4 name4 ActivateSelectSelection等,那就更好了。

替换以下4行:

ActiveChart

有了这两个:

ws1.Range("B1").Select
Set rng = Range(Selection, Selection.End(xlToRight)).Select ' Error Ocurring here
ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.SetSourceData Source: rng.Select