Microsoft KB Archive/213224

From BetaArchive Wiki
Knowledge Base


XL2000: Visual Basic Example for Using a Spin Button with a Date

Article ID: 213224

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Excel 2000 Standard Edition



This article was previously published under Q213224


SUMMARY

In Microsoft Excel 2000, you can use a SpinButton control on a UserForm to change values on a worksheet or in a Visual Basic procedure. Microsoft Visual Basic for Applications in Excel 2000 provides the ability to attach procedures to object events such as clicking on the DOWN ARROW of the spin button (SpinDown event) and clicking on the UP ARROW (SpinUp event).

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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

To use a SpinButton control to both increment and decrement a date value in an edit box, follow these steps:

  1. From a new workbook in Microsoft Excel 2000, press ALT + F11 to start the Visual Basic Editor.
  2. In the Microsoft Visual Basic Editor, click Insert, and then select UserForm.
  3. Click the TextBox tool in the Controls Toolbox. Place a text box (TextBox1) on the UserForm.
  4. Click the SpinButton tool in the Controls Toolbox. Place a spin button (SpinButton1) on the UserForm.
  5. Click the CommandButton tool in the Controls Toolbox. Place a command button (CommandButton1) on the UserForm.
  6. On the View menu, click Properties Window.
  7. With CommandButton1 selected, click Caption, type Close Now, and then press ENTER. This places the text on the command button.
  8. Select the UserForm by clicking anywhere on the form. In the Properties window, click Caption, type Change Date, and then press ENTER.
  9. Right-click the command button, and then click View Code. Two lines of code are automatically entered for you. Between those two lines, type the following:

    UserForm1.Hide
                        
  10. Double-click UserForm1 (UserForm) on the Window menu to return to the UserForm.
  11. On the UserForm, right-click the SpinButton, and then click View Code.
  12. Ensure that the Object list box (the drop-down list box in the upper-left of the window) shows SpinButton1. Click the Procedures list box (the drop-down list box in the upper-right of the window) and select SpinDown. Type the following code between the two lines placed automatically on the sheet.

    UserForm1.TextBox1.Text = DateValue(UserForm1.TextBox1.Text) - 1
                        
  13. Click the Procedures list box, and then select SpinUp. Type the following code between the two lines of code placed automatically on the sheet.

    UserForm1.TextBox1.Text = DateValue(UserForm1.TextBox1.Text) + 1
                        
  14. In the Visual Basic Editor, click Insert, and then click Module. On the module sheet, type the following code:

    Sub ShowForm()
        UserForm1.TextBox1.Text = Date
        UserForm1.Show
    End Sub
                        
  15. In the Visual Basic Editor, click Tools, and then click Macros.

    In the Macros dialog box, click ShowForm, and then click Run.

The UserForm appears. Click the UP ARROW to increment the date. Click the DOWN ARROW to decrement the date. Click the Close Now button to close the UserForm, and then return to the Visual Basic Editor.

REFERENCES

For more information about the SpinButton control, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type SpinButton control in the Office Assistant or the Answer Wizard, and then click Search to view the topic.



Additional query words: XL2000

Keywords: kbdtacode kbhowto kbinfo kbprogramming KB213224