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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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
To create and use a sample Visual Basic macro that uses a spinner
control to increment and decrement a date value in an edit box, follow these steps:
- Start Excel.
- In a new workbook, right-click any sheet tab.
- On the shortcut menu, click Insert.
- In the Insert dialog box, click MS Excel 5.0 Dialog, and then click OK.
- Click the Edit Box tool on the Forms toolbar, and then place an edit box on the dialog sheet.
- Click the Spinner tool on the Forms toolbar, and then place a spinner on the dialog sheet.
- Press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module.
- On the module sheet, type or paste the following code:
' Defines variable called "OldSpin" as an integer, and makes it
' available to all subroutines.
Public OldSpin As Integer
' This routine is assigned to the spinner to run when you choose the
' spinner. It will determine the difference between the current
' spinner value and OldSpin, and use the difference to increment or
' decrement the date in the edit box.
Sub SpinDate()
' Sets the edit box value based on the difference
' between OldSpin and the spinner value.
DialogSheets(1).EditBoxes(1).Text = _
DateValue(DialogSheets(1).EditBoxes(1).Text) - OldSpin + _
DialogSheets(1).Spinners(1).Value
' Resets OldSpin to the spinner value.
OldSpin = DialogSheets(1).Spinners(1).Value
End Sub
' This routine initializes the spinner and OldSpin to a value of
' 15000, populates the edit box with today's date, and displays the
' dialog box.
Sub ShowDialog()
' Sets OldSpin to 15000.
OldSpin = 15000
' Sets the current spinner value to 15000.
DialogSheets(1).Spinners(1).Value = OldSpin
' Sets the edit box to today's date.
DialogSheets(1).EditBoxes(1).Text = Date
' Displays the dialog box.
Dialogsheets(1).Show
End Sub
- Press ALT+F11 to return to Excel.
- Click to select the dialog sheet.
- Right-click the spinner control on the dialog sheet, and then click Assign Macro on the shortcut menu.
- Click the SpinDate macro, and then click OK.
- On the Tools menu, point to Macro, and then click Macros.
- Click the ShowDialog macro, and then click Run.
The spinner increments or decrements the date in the edit box. (By default,
this date is the current date.) You can manually type a different date.