Microsoft KB Archive/151498

= Using VBA Code to Make a Spinner Display Noninteger Values =

Article ID: 151498

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q151498



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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements. 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:  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. Use the tools on the Forms toolbar to create an edit box and a spinner on the dialog sheet. Select the Spinner control. On the Format menu, click Object or Control.

 Set Minimum Value to 0. Set Maximum Value to 16. Set Incremental Change to 1, and then click OK.</ol> </li>  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 </li></ol>

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.

<div class="references_section">