XL: Using VBA Code to Make a Spinner Display Noninteger Values

ID: Q151498


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition


SUMMARY

A spinner control only uses integer values. This article shows how to use Microsoft Visual Basic for Applications code to make a spinner control display noninteger values in an edit box.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
The following sample Visual Basic macro demonstrates how to use a spinner control to return the values 0-4 in .25 increments to an edit box. To create the sample dialog box and macro code, follow these steps:
  1. In a new workbook, click Macro on the Insert menu, and then click Dialog.

    In Microsoft Excel 97 or Microsoft Excel 98, right-click (or CONTROL- click) a sheet tab. On the shortcut menu, click Insert. In the Insert dialog box, select MS Excel 5.0 Dialog. Click OK.


  2. Use the tools on the Forms toolbar to create an edit box and a spinner on the dialog sheet.


  3. Select the Spinner control. On the Format menu, click Object or Control.

    1. Set Minimum Value to 0.


    2. Set Maximum Value to 16.


    3. Set Incremental Change to 1, and then click OK.




  4. On the Insert menu, click Macro, and then click Module. (In Microsoft Excel 97 or Microsoft Excel 98, point to Macro on the Tools menu, and then click Visual Basic Editor. On the Insert menu, click Module.) In the new module sheet, type the following macro code:
    
          Sub ShowDialog()
              ' Shows the dialog box.
              DialogSheets(1).Show
          End Sub
    
          Sub SpinFraction()
              ' Changes the value in the edit box to 1/4 of the spinner value.
              DialogSheets(1).EditBoxes(1).Text = _
                  DialogSheets(1).Spinners(1).Value / 4
          End Sub 


After you enter this code, you will have two macros in the module: ShowDialog and SpinFraction. You will use these macros with the dialog box that you created in step 1.

To assign the SpinFraction macro to the spinner, follow these steps:

  1. Select the spinner control. On the Tools menu, click Assign Macro.


  2. In the Assign Macro dialog box, click the SpinFraction macro, and then click OK.


To run the ShowDialog macro, follow these steps:
  1. On the Tools menu, click Macro.


  2. In the Macro dialog box, click the ShowDialog macro, and then click Run.


The spinner will increment or decrement the value in the edit box by .25 each time you click the spinner.


REFERENCES

For more information about Spinners in Microsoft Excel 97, from the Visual Basic Editor, click the Office Assistant, type SpinButton, click Search, and then click to view "SpinButton Control."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

Q176476 OFF: Office Assistant Not Answering Visual Basic Questions
For more information about spinners in Microsoft Excel 5.0, search on the word "Spinner" using Visual Basic Help.

Additional query words: 5.00a 5.00c 7.00a XL98 XL97 XL7 XL5

Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : MACINTOSH:5.0,5.0a,98; WINDOWS:5.0,5.0c,7.0,7.0a,97; winnt:5.0
Platform : MACINTOSH WINDOWS winnt
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.