I have written some code that cycles through a series of data and adds values that meet certain criteria to various collections. I'm now wanting to change the X & Y values and Data Labels of my chart to reflect those in the collections, but am unsure how to do this. I've previously only used ranges for this logic, which works, and I suppose I could write the values in the collection to cells on Sheet15 and refer to the ranges that way, but it seems like there should be a better solution. Thanks in advance
Code:
Option ExplicitSub Update_Chart()
Application.ScreenUpdating = False
Dim Sector As String, SubSector As String
Dim ws As Worksheet
Dim rws As Integer, clmns As Integer, i As Integer
Dim XValuesCollection As Collection, YValuesCollection As Collection, LabelsCollection As Collection
Dim cht As Object, srs As Variant
Set XValuesCollection = New Collection
Set YValuesCollection = New Collection
Set LabelsCollection = New Collection
With Sheet15
Sector = .Cells(2, 3)
SubSector = .Cells(3, 3)
End With
For Each ws In ThisWorkbook.Worksheets
If ws.Name = Sector Then
With ws
rws = .Cells(Rows.Count, 2).End(xlUp).Row
clmns = .Cells(2, Columns.Count).End(xlToLeft).Column
For i = 3 To rws
If .Cells(i, Application.Match("Sub-Sector", .Rows(2), 0)) = SubSector Then
XValuesCollection.Add (.Cells(i, Application.Match("TTM Revenue", .Rows(2), 0)).Value)
YValuesCollection.Add (.Cells(i, Application.Match("P/E", .Rows(2), 0)).Value)
LabelsCollection.Add (.Cells(i, Application.Match("Ticker", .Rows(2), 0)).Value)
End If
Next i
End With
GoTo Jumpout
End If
Next ws
Jumpout:
With Sheet15
For Each cht In ws.ChartObjects
For Each srs In cht.Chart.SeriesCollection
With srs
.XValues = XValuesCollection
.Values = YValuesCollection
With .DataLabels
.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, LabelsCollection, 0
End With
End With
Next srs
Next cht
End With
Application.ScreenUpdating = True
End Sub