A member of the MrExcel forum wanted to know about using form controls to select columns from a data table to populate a graph. Essentially he wanted his users to be able to choose between two charts, one comparing their scores to district and regional scores, the other comparing their scores to the top 20%. This is a retelling of my suggested approach to his question.
Here is some dummy data I used for his problem, and a chart showing all of the data.
My suggested approach will add some columns (F:I) to the existing data range with formulas that show or don’t show data based on which of two option buttons is selected, and my chart will use all of this data. When the formulas don’t show the values, they will not appear in the chart.
The extended data range is shown below. At first the formula in cell F2 (and copied into the entire range F2:I13) is simply
To give the user a choice, add two option buttons and label them Chart A and Chart B. Right click on one of them (doesn’t matter which) and select Format Control, and on the Control tab, click in the Cell Link box, and select cell K1. This cell shows which of the buttons is selected, 1 or 2.
You don’t need to show all of this to the user. You could put the chart and option buttons on the active sheet, and all of the data (and the option button linked cell) can go onto another sheet, and you can hide this other sheet if you want. Or you can place the original data on the same sheet as the chart and option buttons, and the formulas onto another sheet, a hidden sheet if desired. You could replace the two option buttons with one listbox that shows both options; the linked cell works the same way.
Set up the chart first, before adjusting the formulas to show the result of the option button selection.
Select A1:A13, then hold Ctrl while selecting F1:I13, and insert a line chart.
Delete the legend, because it will always show all series, even if some don’t appear because of the formulas. Instead, label the last point in each series with the series name, and use label text that matches each series color. This is more user-friendly than using a legend, anyway. (I also shortened the axis tick labels so they could all be displayed horizontally.)
Now adjust the formulas. Keep the formulas in column F the way they are, since this series appears whichever option is selected. The formula in G2 (copied into G2:H13) is
This means if the first option button is selected, show the corresponding value, otherwise show #N/A. #N/A isn’t plotted in a line or scatter plot.
Similarly the formula in I2 (and filled into I2:I13) is
That is, if the second option button is selected, show the value, otherwise show #N/A.
The Finished Product
Here is the data and chart if the Chart A option button is selected:
Here is the data and chart if the Chart B option button is selected:
You can download a sample workbook by clicking on this link: MrExcelOptionButtonChart.xlsx.