Hi guys,
I have been trying to figure out (unsuccessfully) how to modify the VBA code that microsoft suggested on their site http://support.microsoft.com/kb/213750
Ideally, what i am trying to do is to avoid having the user select the range of labels ahead of running the code as the text I would like on the labels is in a dynamic defined range (let's call it xDateLabel).
Here is what the code I have so far. this works when i select the cells that contain the Date that I would like the points to have, but I am not savvy with VBA syntax so I dont' know how to just point it to the range name xDateLabel.
Any guidance will be greatly appreciated.
Additionally, I have another series within the same chart that also needs to be labeled (using the same method), I guess I need to create two different strings, or can i use the same and redim it?
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
ActiveSheet.ChartObjects("Chart 1").Activate
'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(6).Formula
'Set xVals = ActiveSheet.Range("xDateLabel")
'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(6).Points(Counter).HasDataLabel = True
ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Text = Format(Range(xVals).Cells(Counter, 1).Offset(0, -1).Value, "mmm-dd")
' ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Text = Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter
End Sub
I have been trying to figure out (unsuccessfully) how to modify the VBA code that microsoft suggested on their site http://support.microsoft.com/kb/213750
Ideally, what i am trying to do is to avoid having the user select the range of labels ahead of running the code as the text I would like on the labels is in a dynamic defined range (let's call it xDateLabel).
Here is what the code I have so far. this works when i select the cells that contain the Date that I would like the points to have, but I am not savvy with VBA syntax so I dont' know how to just point it to the range name xDateLabel.
Any guidance will be greatly appreciated.
Additionally, I have another series within the same chart that also needs to be labeled (using the same method), I guess I need to create two different strings, or can i use the same and redim it?
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
ActiveSheet.ChartObjects("Chart 1").Activate
'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(6).Formula
'Set xVals = ActiveSheet.Range("xDateLabel")
'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(6).Points(Counter).HasDataLabel = True
ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Text = Format(Range(xVals).Cells(Counter, 1).Offset(0, -1).Value, "mmm-dd")
' ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Text = Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter
End Sub