VBA Automatic Labelling

MicoL

New Member
Joined
Apr 3, 2019
Messages
1
Hi all,


I am constructing a panel chart, that is, a chart that has multiple regions which compare similar data sets side by side (in separate panels) rather than right on top of each other.
Essentially a single chart is repeated across a grid, with different data sets in each instance of the chart.


Specifically, I am following the instructions available on the website https://peltiertech.com/Excel/ChartsHowTo/PanelUnevenScales.html trying to generalize them to the case of 4 panels instead of the 3 to which the instructions refer.


According to the example reported on the website, the following vba code should be run in order to automatically add value labels to the vertical axes.

Code:
[CENTER]
[/CENTER]
Sub AttachLabelsToPoints()

   'Dimension variables.
   Dim Counter As Integer, ChartName As String, xVals As String, xVals1 As String, xVals2 As String

   ' Disable screen updating while the subroutine is run.
   Application.ScreenUpdating = True

   'Store the formula for the first series in "xVals".
   xVals = ActiveChart.SeriesCollection(6).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(6).Points(Counter).HasDataLabel = _
         True
      ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Text = _
         Range(xVals).Cells(Counter, 1).Offset(0, 4).Text
   ActiveChart.SeriesCollection(6).Points(Counter).DataLabel.Position = xlLabelPositionLeft
   Next Counter



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

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

   'Attach a label to each data point in the chart.
   For Counter = 1 To Range(xVals1).Cells.Count
     ActiveChart.SeriesCollection(5).Points(Counter).HasDataLabel = _
         True
      ActiveChart.SeriesCollection(5).Points(Counter).DataLabel.Text = _
         Range(xVals1).Cells(Counter, 1).Offset(0, 2).Text
   Next Counter



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

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

   'Attach a label to each data point in the chart.
   For Counter = 1 To Range(xVals2).Cells.Count
     ActiveChart.SeriesCollection(4).Points(Counter).HasDataLabel = _
         True
      ActiveChart.SeriesCollection(4).Points(Counter).DataLabel.Text = _
         Range(xVals2).Cells(Counter, 1).Offset(0, 2).Text
   ActiveChart.SeriesCollection(4).Points(Counter).DataLabel.Position = xlLabelPositionLeft
   Next Counter

End Sub

How can I generalize the code to the case of 4 panels ?
Please consider that my file consists of two sheet: "Sheet-1" with the data of interest that I would like to plot and "Sheet-2" with the chart.
What I would like to do is to run the code for the aumatic labelling from a button placed on Sheet-1.
 
Last edited by a moderator:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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