[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Sub[/COLOR] AddDataLabelsFromRangeToXYChart()
[COLOR=darkblue]Dim[/COLOR] oChart [COLOR=darkblue]As[/COLOR] Chart
[COLOR=darkblue]Dim[/COLOR] oSeries [COLOR=darkblue]As[/COLOR] Series
[COLOR=darkblue]Dim[/COLOR] oDataLabel [COLOR=darkblue]As[/COLOR] DataLabel
[COLOR=darkblue]Dim[/COLOR] rXVals [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]Dim[/COLOR] rLabels [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]Dim[/COLOR] sFormula [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sXValsAddr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] bIsXYChart [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
[COLOR=darkblue]Set[/COLOR] oChart = ActiveChart
[COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] oChart [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
[COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] oChart.ChartType
[COLOR=darkblue]Case[/COLOR] -4169, 72, 73, 74, 75 [COLOR=green]'XY chart types[/COLOR]
bIsXYChart = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] bIsXYChart [COLOR=darkblue]Then[/COLOR]
MsgBox "Select an XY Chart, and try again.", vbExclamation
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
[COLOR=darkblue]Set[/COLOR] oSeries = oChart.SeriesCollection(1)
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
[COLOR=darkblue]If[/COLOR] oSeries [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
MsgBox "No chart series found.", vbExclamation
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
sFormula = oSeries.Formula
sXValsAddr = Split(s[COLOR=darkblue]For[/COLOR]mula, ",")(1)
[COLOR=darkblue]Set[/COLOR] rXVals = Range(sXValsAddr)
[COLOR=darkblue]If[/COLOR] rXVals.Columns.Count = 1 [COLOR=darkblue]Then[/COLOR]
[COLOR=darkblue]If[/COLOR] rXVals.Column = 1 [COLOR=darkblue]Then[/COLOR]
MsgBox "Make sure that the data labels are located one column" & vbNewLine & "to the left of the 'X' column.", vbExclamation
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Set[/COLOR] rLabels = rXVals.Offset(, -1)
[COLOR=darkblue]Else[/COLOR]
[COLOR=darkblue]If[/COLOR] rXVals.Row = 1 [COLOR=darkblue]Then[/COLOR]
MsgBox "Make sure that the data labels are located one row" & vbNewLine & "above the 'X' row.", vbExclamation
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Set[/COLOR] rLabels = rXVals.Offset(-1)
[COLOR=darkblue]End[/COLOR] If
[COLOR=darkblue]With[/COLOR] oSeries
For i = 1 [COLOR=darkblue]To[/COLOR] .Points.Count
.Points(i).HasDataLabel = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]Set[/COLOR] oDataLabel = .Points(i).DataLabel
[COLOR=darkblue]With[/COLOR] oDataLabel
.Text = "=" & rLabels.Cells(i).Address(External:=True)
.Position = xlLabelPositionAbove
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]