I have two Active Combo Boxes , based on the choice of the user (must select two choices) a chart will be generated from data in the dataset below (here is part of the dataset):
When choosing from the two Combo Boxes :
A chart will be generated in the same sheet like below :
Here’s my code but I’m getting a compilation error (qualificateur incorrect
):
Option Explicit
Private Sub Workbook_Open()
Dim cbx As String
Dim cbx2 As String
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Feuil1!A3:B16")
If cbx.Value = "6 mois" And cbx2.Value = "Télévision" Then
ActiveChart.SetSourceData Source:=Range("Feuil1!A3:B16")
//... same code with other conditions
End If
End Sub
Since you have two combo boxes, I assume you want to plot only one series, e.g. television 6 months or cinema 12 months.
The combos are linked to the worksheet, both to the input range and the cell link. It’s easier to use these cell ranges than to try to get the information out of the combos.
Here’s my setup:
Two combo boxes. The left one uses C4:C6 as the input range and B4 as the cell link. The right one uses F4:F6 as the input range and E4 as the cell link. We really only care about B4 and E4, because we can determine which column to use for our Y values from them.
(Normally you might put the input range and cell links somewhere out of the user’s eyesight, but not for this illustration.)
Here’s the code.
Sub InsertChartPerComboBoxes()
Dim rXValues As Range, rYValues As Range, rName As Range
Dim cht As Chart
Dim iComboMonths As Long, iComboMedia As Long
Dim iColumnOffset As Long
iComboMonths = ActiveSheet.Range("B4").Value
iComboMedia = ActiveSheet.Range("E4").Value
iColumnOffset = (iComboMedia - 1) * 3 + iComboMonths
Set rXValues = ActiveSheet.Range("A10:A17")
Set rYValues = rXValues.Offset(0, iColumnOffset)
Set rName = ActiveSheet.Range("A8:A9").Offset(0, iColumnOffset)
Set cht = ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Chart
cht.ChartArea.ClearContents
With cht.SeriesCollection.NewSeries
.Values = rYValues
.XValues = rXValues
.Name = "=" & rName.Address(, , , True)
End With
End Sub
And here is a chart I made after selecting 9 months and Theater with the combo boxes. I’ve selected the series, so you can see the data ranges highlighted in the worksheet.
Answer:
I have an alternative answer, based on a different interpretation of your circumstance.
What if you have the data and a chart, and you just want to be able to select what is in that chart, without building a new chart each time. The following is a great technique for a dashboard that you want to let users interact with, using the combo boxes.
Setup:
Two combo boxes. The left one uses C4:C6 as the input range and B4 as the cell link. The right one uses F4:F6 as the input range and E4 as the cell link.
I’ve added a column next to the data range, which will change based on the selections in the combo boxes.
The formula in cell K8 is =OFFSET(B8,0,(E4-1)*3)
The formula in cell K9 is =OFFSET(A9,0,($E$4-1)*3+$B$4)
Copy cell K9, select K10:K17, and paste. Apply percent formatting to K10:K17.
The series plotted in the chart uses A10:A17 for its X values, K10:K17 for its Y values, and K8:K9 for its name. The series formula always points to these ranges, and the formulas change the values when the combo boxes change. I’ve selected the series so you can see its source data highlighted in the worksheet.
Tags: charts, excelexcel