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
Comments

John,

You can simulate this approach without VBA. Just set up your spinbutton with min value 0 and max value 100. Link the spin button to cell D1. Then in cell A1, enter the formula “=D1/100” and format as a percent. Now you have the same thing without having VBA in your workbook. Personally I avoid it if possible because now with VBA in it, you get the security prompt every time you open the workbook.

Also, your procedures hard-code the linked cell value (C4) in them. If you change the linked cell, you have to remember to change the references in the procedures. Wouldn’t it be better to use the LinkedCell property instead?

FYI your blog is a bit hard to navigate. I click on the post title in Google Reader and it takes me to a home page instead of the article. The article is linked there, but clicking on the article name just reloads the page. I have to click on “cM Blog” in the navbar and then scroll to find the article.

Posted by JP  on  02/23  at  07:27 AM

JP,

I agree with you . . . setting the SpinButton up as you’ve described is probably the easiest and most efficient way to do it.  I should have noted this in the post, but the reason that I presented the use of VBA is primarily because most of the models that I’m working with might have 50, 100, or even more SpinButtons per sheet.  With a large amount of SpinButtons, I prefer to use VBA because I can quickly see all of the properties that I want to reference within a single sheet module.  I’ll usually set up named ranges for the linked-cell range for each SpinButton and then refer to those named range in the code rather than referencing absolute cell addresses as presented in the post.  And because VBA allows you to use variables, I can change the properties of multiple SpinButtons as once as opposed to changing each one individually.

Thanks for the feedback concerning the RSS feed.  I have a comment link that appears at the bottom of each RSS post that takes you directly to the post on the site.  However, I didn’t realize the linked title was going to the home page so I’ll be making that change to the template soon.  Thanks again for catching that and letting me know.

I’ve spent a lot of time on your site and it’s a great resource.  Keep up the good work.

John

Posted by .(JavaScript must be enabled to view this email address)  on  02/25  at  04:10 AM

I must say that adding it Makes it easier to increase or decrease a value, such as a number increment, time, or date. To increase the value, click the up arrow; to decrease the value, click the down arrow. A user can also type a text value directly in the associated cell or text box. Use a spin button, for example, to make it easier to enter a month, day, year number, or to increase a volume level.
—————

Posted by ccent  on  05/28  at  09:45 PM

Nice, I never knew you could add a spinbutton a spreadheet file.

Posted by Andy  on  06/11  at  07:21 AM

It’s so hard for me to use programs and to add new tools. Your explanation is very clear. Thanks for sharing.

Posted by Russian Girl  on  07/04  at  08:23 AM

Yes it’s great and I was not aware of it but your clear explanation really helped me.

Posted by PHP Development  on  07/08  at  12:16 AM
Page 1 of 1 pages

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: Simple Rollover Calculation

Previous entry: Excel 2010 Chart Improvements - Migrating Excel 4 Macros to VBA

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 28

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