Data Point Labels on Filtered Source Range

Matt W

New Member
Joined
Aug 31, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello. I am trying to assign labels to a bubble chart based on a filtered range of data. I have leveraged the code below from Jon Peltier to do just that. However, in my use-case the code only returns the label for the first data point in the series, and fails to loop after that. I can't figure out why. It fails to loop the source range whether there are hidden rows or not. The source range is a table and sits in a worksheet different than the chart.

Any help would be much appreciated! Thanks!

VBA Code:
Sub AttachLabelsToBubbles()

'Dimension variables.
Dim Counter As Integer, ChartName As String, xVals As String
Dim lngChtCounter As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Profile")

ws.ChartObjects("ProfileChart").Activate

'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(1).Formula

'Extract the range for the data from xVals.
xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
Do While Left(xVals, 1) = ","
xVals = Mid(xVals, 2)
Loop

'Attach a label to each data point in the chart.
With ActiveChart.SeriesCollection(1)
For Counter = 1 To Range(xVals).Cells.Count
If Not Range(xVals).Cells(Counter, 1).EntireRow.Hidden Then
lngChtCounter = lngChtCounter + 1
.Points(lngChtCounter).HasDataLabel = True
.Points(lngChtCounter).DataLabel.Text = Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
End If
Next Counter
End With

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
After some further analysis...if I'm understanding the code correctly as I step through it, it seems the xVals variable is resolving to the first row of the source range, such that the line of code

VBA Code:
For Counter = 1 To Range(xVals).Cells.Count

appears to count the range as 1 to 1; hence the code stops after returning the first label? Not sure if that's what's really happening, but it seems like it.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top