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

Last reviewed: February 3, 1998
Article ID: Q151498
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for 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 can only be set to an integer value. This article shows how to use Microsoft Visual Basic for Applications code to make a spinner 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 engineers 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/refguide/default.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.

    a. Set Minimum Value to 0.

    b. Set Maximum Value to 16.

    c. 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
    
    
Once you enter this code, you will have two macros in the module: ShowDialog and SpinFraction. These macros will be used with the dialog box that you created in step 1.

To assign the SpinFraction macro to the spinner:

  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:

  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, search on the word "Spinner" using Visual Basic Help.


Additional query words: 5.00 5.00a 5.00c 7.00 7.00a XL98 XL97 XL7 XL5
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.