Microsoft KB Archive/151498

From BetaArchive Wiki
Knowledge Base


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:

  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:

176476 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 XL

Keywords: kbdtacode kbhowto kbprogramming KB151498