Microsoft KB Archive/161517

= XL97: Visual Basic Example for Using SpinButton with a Date =

Article ID: 161517

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q161517





SUMMARY
In Microsoft Excel 97, you can use a SpinButton on a UserForm to change values on a worksheet or in a Visual Basic procedure. Microsoft Visual Basic for Applications in Excel 97 provides the ability to attach procedures to object events such as clicking on the down arrow of the SpinButton (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:

https://partner.microsoft.com/global/30000104

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

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

The following Visual Basic procedure demonstrates how to use a SpinButton control to both increment and decrement a date value in an edit box.

 From a new workbook in Microsoft Excel 97, press ALT + F11 to activate the Visual Basic Editor. In the Microsoft Visual Basic Editor, click Insert and choose UserForm. Choose the TextBox tool from the Controls Toolbox. Place a TextBox (TextBox1) on the UserForm. Choose the SpinButton tool from the Controls Toolbox. Place a SpinButton (SpinButton1) on the UserForm. Choose the CommandButton tool from the Controls Toolbox. Place a CommandButton (CommandButton1) on the UserForm. On the View menu, click Properties Window.</li> With CommandButton1 selected, click the Caption and type Close Now and press ENTER. This places the text on the CommandButton.</li> Select the UserForm by clicking anywhere on the form. In the Properties Window, click Caption, type Change Date, and press ENTER.</li> Right-click the CommandButton and choose View Code. Two lines of code are automatically entered for you. Between those two lines, type the following:

UserForm1.Hide

</li> Click "UserForm1 (UserForm)" on the Window menu to return to the UserForm.</li> On the UserForm, right-click the SpinButton and choose View Code.</li>  Ensure that the Object dropdown (the dropdown in the upper left of the window) shows SpinButton1. Click the Procedures dropdown box (the dropdown in the upper right of the window) and choose SpinDown. Type the following code between the two lines placed automatically on the sheet. UserForm1.TextBox1.Text = DateValue(UserForm1.TextBox1.Text) - 1 </li>  Click the Procedures dropdown box and choose SpinUp. Type the following code between the two lines of code placed automatically on the sheet. UserForm1.TextBox1.Text = DateValue(UserForm1.TextBox1.Text) + 1 </li>  In the Microsoft Visual Basic Editor, click Insert and choose Module. On the module sheet, type the following code: Sub ShowForm UserForm1.TextBox1.Text = Date UserForm1.Show End Sub </li> In the Microsoft Visual Basic Editor, click Tools and choose Macros. In the Macros dialog, click ShowForm and then click Run.</li></ol>

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 return to the Microsoft Visual Basic Editor.

<div class="references_section">