Working with controls

I use the standard Excel controls (that's the Forms toolbar) all the time. You can also use the controls from the Visual Basic toolbar, but they usually require VBA programming.

I'm going to assume you know how to put a control onto a worksheet, and provide some useful (I hope) tips on working with controls.

You can put any of the controls on the Forms toolbar onto a worksheet, except the Textbox.

Label control is most useful for labelling other controls. You can't change the font style (if you want to do this, use a textbox from the Drawing toolbar).

Frame control is good for putting around other controls, and is, I believe, the only way to include more than one set of option buttons on a sheet. If you put some option buttons within a frame, then they will all work together (ie only one can be turned on a time), and you can put another set of option buttons somewhere else on the sheet and they will work completely separately.

Button control is good for giving the user something to click, but needs code behind it.

Checkbox control is good for giving users options. It doesn't need code.

Radio button control is good for giving users options. Use together with the frame control if you have more than one set of radio controls. It doesn't need code.

Listbox control lets users choose from a list. It doesn't need code, unless you configure it to allow multiple user selections.

Combobox control lets users choose from a list. It doesn't need code.

Slider control lets users choose from a range of settings (eg from low to high). It doesn't need code.

The reason many of the controls don't need code is that you can configure them to put the value selected by the user into a specific worksheet cell, and you can then write formulae that use the value from that cell. Alternatively, you can use VBA to read the user selection straight from the control, ie you don't need to specify a cell to show the user selection if you don't need it.

Tips for using listboxes and comboboxes

First, you can populate the list either from a column of cells on the worksheet, or from VBA code. If you do it from a worksheet list, you may want the list to dynamically adjust itself when you add or remove items. Here's how.

Let's suppose our list is in the cells C8 to C20, but we may not need to use all those cells, ie this is the maximum number of items we will need. Create a rangename (Insert, Name, Define) with a name of (say) List1, and in the RefersTo textbox, put this formula: =OFFSET($C$8,0,0,COUNTA($C$8:$C$20),1) and press the Add button. Format the listbox (or combobox) to use the named range by putting List1 in the Input range textbox, as shown below.

The reason this works is that the formula counts the number of filled cells in the range and just returns those cells. If you populate the list from code, then it will be saved with the workbook, and you don't have to redo it when you open the workbook the next time.

One advanced way you could use this technique is that when user selects an item in one listbox, the items shown in a second listbox change dynamically. This is easily done using formulae in the cells used for the second list to show different lists of items depending on the selection in the first listbox, and the unused list items can be set blank.

Note: this trick also works well if you want your charts to adjust dynamically when data is added or removed. I do mention it in the Charts section.

Preventing controls from resizing

If you find your controls resizing themselves when you change cell height or width, format the control, and in the Properties tab, tick the item "Don't move or size with cells" or "Move but don't size with cells".

 

Note: Where possible, I've attributed ideas to the people who thought of them. If I have missed someone or made a mistake, please let me know.