I'm trying to edit a pivot chart so that its data labels are scaled down where necessary (i.e. 6 digit numbers are divided by 1,000), which requires the addition of custom labels from a cell range to the number to show that it's been scaled. For example, a number that was originally 800,000 will be converted to 800 within the PivotTable via a custom formula series, and then another cell with a formula in it will determine whether it's been scaled and show an "M" (for mile) if it has.
I can accomplish this statically without VBA simply by checking the "Value from Cells" box in addition to the "Value" box when formatting the Data Labels. However, changing the view, as I need to do constantly, resets the link I made when checking "Value from Cells." Additionally, my labels frequently revert back to being un-bolded and smaller than I made them. Therefore, I feel like I need to create a macro that will update my data labels to include a range lookup and resizing with every change to the table. My code to this end is below.
Dim numSubs As Integer
Dim Labels As Range
numSubs = 7 'find a way to count number of active series
For i = 1 To numSubs
With Sheet16.ChartObjects("Chart 2").SeriesCollection(i).DataLabels
.ShowRange = False
.Format.TextFrame2.TextRange. _
InsertChartField msoChartFieldRange, "='New PC Mapping'!$I$32:$I$34", 0 'find a way to cycle thru ranges
.Format.TextFrame2.TextRange.Font.Bold = msoTrue
.Format.TextFrame2.TextRange.Font.Size = 14
End With
Next
First, VBA is telling me I can't run a SeriesCollection on my chart, which is the method shown in all the examples I've read. I have no idea what the sticking point is here. I've verified that it can identify Sheet16.ChartObjects("Chart 2")
Second, I need to find a way to cycle through multiple ranges, as each series has its own. My line of code here is from recording a macro because I couldn't find code for doing this anywhere online, so I'm sure it can be improved, but in the current form it's taking a string of the formula, which makes it difficult to move with a Range.Offset, for example.
From there, I know how to trigger it when the PivotTable is addressed. I just need some help getting it to work on just one series and I can generalize from there. Thanks!
Sheet16.ChartObjects("Chart 2").SeriesCollection(i).DataLabelstoSheet16.ChartObjects("Chart 2").Chart.SeriesCollection(i).DataLabelsought to give you the desired result. [1]: docs.microsoft.com/en-us/office/vba/api/excel.seriescollection - Chris Meurer