Hi all,
Thanks for reading. I am very new to VBA but am hoping with some help I can pull this off.
My table has 4 columns (Category, Label, X, and Y). I am creating a scatter plot (X and Y) and using a macro to assign data labels (Label) to the points. However, when I filter (on Category) the labels are static and do not change with the data in the table. The labels do not correlate to the data point they are placed on. If I have a list of 10 items and filter it down to 3, the first 3 labels (labels in rows 1-3) will be assigned to those points even if they were items 8-10.
I am looking for a solution to have the labels stay with the points as the data is filtered. Any tips would be appreciated.
I hope this makes sense and that someone can help me solve this. The code is below, taken from a tutorial.
Sub AttachLabelsToPoints()
'Dimension variables.
Dim Counter As Integer, ChartName As String, xVals As String
' Disable screen updating while the subroutine is run.
Application.ScreenUpdating = False
'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.
For Counter = 1 To Range(xVals).Cells.Count
ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
True
ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter
End Sub
Bookmarks