Labelling data point on series

viv_ien

New Member
Joined
Feb 19, 2018
Messages
2
Hi!

I am collating some feedback which I need to present in line graphs which include the data table showing the FY and the no. of response. As such, I must use the "name" column as the series name when creating these graphs.

In addition, I have been using a VBA that I found online to label the last data point of the graphs. However, the points are labelled with the "name" column. Instead, I would like the points to be labelled with the text from the "Financial Year" column (i.e. FY13, FY14 etc).

Financial YearResponseNameQ1Q2
FY1310FY13 (n = 10)2.23.4
FY1420FY14 (n = 20)1.53.1
FY15 1st Half5FY15 1st Half (n = 5)3.62.8

<tbody>
</tbody>

I am completely new to VBA and have been tearing my hair out over this. Is it possible to edit the VBA below to label the data points with the data from the "financial year" column rather than the series name?

==========
Sub LabelLastPoint()
Dim mySrs As Series
Dim nPts As Long
For Each mySrs In ActiveChart.SeriesCollection
With mySrs
nPts = .Points.Count
mySrs.Points(nPts).ApplyDataLabels _
Type:=xlDataLabelsShowValue, _
AutoText:=True, LegendKey:=False
mySrs.Points(nPts).DataLabel.Text = mySrs.Name
End With
Next
End Sub
===========
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi viv_ien and Welcome to the Board. Assuming data starts in sheet1 A2 this should work. HTH. Dave
Code:
Sub LabelLastPoint()
 Dim mySrs As Series
 Dim nPts As Long, Cnt As Integer
 Cnt = 1
 For Each mySrs In ActiveChart.SeriesCollection
 Cnt = Cnt + 1
 With mySrs
 nPts = .Points.Count
 mySrs.Points(nPts).ApplyDataLabels _
 Type:=xlDataLabelsShowValue, _
 AutoText:=True, LegendKey:=False
 mySrs.Points(nPts).DataLabel.Text = CStr(Sheets("Sheet1").Range("A" & Cnt).Value)
 End With
 Next
 End Sub
ps. please use code tags
 
Upvote 0
Thanks for the help, Dave :) I managed to get it to work after tweaking the integer number.

Would just like to check if there is any way to further adjust the VBA such that it will take the data two columns left of whatever data that I have selected to be displayed on the graph?

The background is that I have multiple worksheets, which contains a few tables (and graphs) on each worksheet. Every year, I will add on new information to the table, increasing the number of rows for my tables. While I need to keep all these information, I only need to present the recent 3 years information in the graph form. As such, I will manually adjust the data selected for the graph where necessary. With the current VBA, I will have to manually adjust the integer number every year when I add new information and adjust the data selection for the graphs.

Is this possible?
 
Upvote 0
Not sure if I understand exactly what U want (and I hate using selection) but U can trial this. Dave
Code:
Sub LabelLastPoint()
 Dim mySrs As Series, ColNum As Integer
 Dim nPts As Long, Cnt As Integer, Rng As Range
 ColNum = 0
 On Error Resume Next
 Set Rng = Selection.Offset(0, -3)
 If Err Then MsgBox "Make valid selection!": Exit Sub
 On Error GoTo 0
 ColNum = Rng.Column
 If ColNum = 0 Then
 MsgBox "Select something valid!"
 Exit Sub
 End If
 Cnt = 1
 For Each mySrs In ActiveChart.SeriesCollection
 Cnt = Cnt + 1
 With mySrs
 nPts = .Points.Count
 mySrs.Points(nPts).ApplyDataLabels _
 Type:=xlDataLabelsShowValue, _
 AutoText:=True, LegendKey:=False
 mySrs.Points(nPts).DataLabel.Text = CStr(Sheets("Sheet1").Cells(Cnt, ColNum).Value)
 End With
 Next
 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,561
Members
449,038
Latest member
Guest1337

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