cellMatrix.net

Spreadsheet Modeling and Related Topics

Add an ActiveX SpinButton Control to a Spreadsheet

One of my favorite tools for controlling the input to a single cell within an interactive spreadsheet model is an ActiveX SpinButton. When added to a spreadsheet, the SpinButton can allow the user to move quickly through multiple scenarios within a predefined range of values.

In Excel 2007, you can add an ActiveX SpinButton to a spreadsheet by first selecting the Developer tab. Then select Insert -> ActiveX Controls -> Spin Button.

image

In Design mode, you can link a cell as well as adjust the minimum and maximum values for the SpinButton. To do so, click on the SpinButton when in design mode. Then right-click on your mouse to access the properies for that SpinButton.

Rather than manually entering the minimum, maximum, and linked cell properties, my personal preference is to use a few simple VBA statements. For example, assuming the SpinButton is named "SpinButton1", to add a maximum value = 30, a minimum value = 20, and a linked cell = C4, and an incremental value = 1, add the following code to the sheet module for the sheet containing the ActiveX SpinButton.

Private Sub SpinButton1_SpinDown()
    With Range("C4")
        .Value = WorksheetFunction.Max(20, .Value - 1)
    End With
End Sub

Private Sub SpinButton1_SpinUp()
    With Range("C4")
        .Value = WorksheetFunction.Min(30, .Value + 1)
    End With
End Sub

The cell entry for the SpinButton combined with the SpinButton Control might be set up on the spreadsheet to look like this:

image

To allow the SpinButton to accept changes in percent, slightly modify the code above. For example, to add a maximum value = .50, a minimum value = .30, and a linked cell = C4, and an incremental value = .01, add the following code to the sheet module for the sheet containing the ActiveX SpinButton.

Private Sub SpinButton1_SpinDown()
    With Range("C4")
        .Value = WorksheetFunction.Max(0.3, .Value - 0.01)
    End With
End Sub

Private Sub SpinButton1_SpinUp()
    With Range("C4")
        .Value = WorksheetFunction.Min(0.5, .Value + 0.01)
    End With
End Sub

After changing the cell entry number format to percent, for the SpinButton combined with the SpinButton Control might be set up on the spreadsheet to look like this:

image

Although you must be in design mode to modify the size and position of the SpinButton and /or modify its properties, you must exit the design mode in order for the SpinButton to be operable.

Note: the examples above are based on code and techniques that can be found at the following:


Posted on Tuesday, February 23, 2010 | Comments (6) | Permalink

Page 1 of 1 pages

Statistics

  • Total Entries - 136
  • Current Viewers - 26

Categories

Entries by Day

Jul - 2010
S M T W T F S
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Recent Comments

Syndicate