简体   繁体   English

Excel VBA将数据添加到图表

[英]Excel VBA adding data to a chart

Hello I have a little question for adding data to an existing chart. 您好,我有一个将数据添加到现有图表中的问题。

Now I have a worksheet containing a data series with months for the years in the 2nd row of the sheet. 现在,我在工作表的第二行中有了一个工作表,其中包含一个年份为月份的数据系列。 So the months are for example B2 1.2017, C2 2.2017, and in the rows 3,4,5,6,7 and 8 there is always data for that month. 因此,例如,月份是B2 1.2017,C2 2.2017,并且在第3、4、5、6、7和8行中始终有该月份的数据。

Now I just want my macro to add the new Month plus the data of the rows below to my existing chart. 现在,我只希望宏将新的Month以及下面各行的数据添加到现有图表中。

the code I have so far is this: 我到目前为止的代码是这样的:

Worksheets("Summary").ChartObjects("Chart").Activate
   ActiveChart.SeriesCollection.Add _
   Source:=Worksheets("Summary").Range("B2:B8")

now this does just create new data series but there is actually no new data added to the chart. 现在这只是创建新的数据系列,但实际上没有向图表添加任何新数据。

The code below might seem a little long, but it's the safest way to add a new Series with Data to an existing Chart. 下面的代码可能看起来有些长,但这是向现有图表添加带有数据的新Series的最安全方法。

I'm setting all the necessary Objects so the code will be as "safe-proof" as can be. 我设置了所有必需的Objects因此代码将尽可能地“安全”。

Code

Option Explicit

Sub AddSeriestoChart()

Dim ws As Worksheet
Dim ChtRng As Range
Dim ChtObj As ChartObject
Dim Ser As Series

' set the Worksheet object
Set ws = ThisWorkbook.Worksheets("Summary")

' Set the Chart Object
Set ChtObj = ws.ChartObjects("Chart")

' Set the Range of the Chart's source data
Set ChtRng = ws.Range("B2:B8")

With ChtObj
    ' add a new series to chart
    Set Ser = .Chart.SeriesCollection.NewSeries

    ' set the source data of the new series
    Ser.Values = "=" & ChtRng.Address(False, False, xlA1, xlExternal)
End With

End Sub

Edit 1 : to modify existing Series data, use something like the code below : 编辑1 :要修改现有的Series数据,请使用以下代码:

With ChtObj          
    For i = 1 To .Chart.SeriesCollection.Count
        Set Ser = .Chart.SeriesCollection(i)

        ' set the source data of the new series
        Set ChtRng = ws.Range("B" & i + 2)
        Ser.Values = "=" & ChtRng.Address(False, False, xlA1, xlExternal)

        Set ChtRng = Nothing
    Next i
End With

This is what I would use 这就是我会用的

wsMetric.ChartObjects("Chart").Chart

'This one will link data from another workbook
.SeriesCollection(1).Values = "='[" & wb.Name & "]" & ws.Name & "'!$" & sCol & "$" & lRow  & ":$" & sCol2 & "$" & lRow2 
'Debug.Print "='[" & wb.Name & "]" & ws.Name & "'!$" & sCol & "$" & lRow  & ":$" & sCol2 & "$" & lRow2 'Returns ='[Book1.xlsm]Sheet1'!$A$1:$A$11


'This one will link data from the same workbook, same or different sheet
.SeriesCollection(1).Values = "=" & ws.Name & "!$" & sCol & "$" & lRow & ":$" & sCol2 & "$" & lRow 2
'Debug.print "=" & ActiveSheet.Name & "!$" & scol & "$" & lrow & ":$" & scol2 & "$" & lrow2 'Returns =Sheet1!$A$1:$A$11
End With

This doesn't use .Activate and directly accesses the chart 这不使用.Activate并直接访问图表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM