Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Programmatically Increment a Date With a Spinner


View products that this article applies to.

This article was previously published under Q213464

↑ Back to the top


Summary

In Microsoft Excel, you can use a spinner control to change values on a worksheet or in a Microsoft Visual Basic for Applications procedure. To use the spinner to increment and decrement a value over a wide range, you must set and use the spinner in conjunction with another value or cell to calculate both positive and negative values.

This article describes a Visual Basic macro that uses a spinner to increment or decrement a date in an edit box.

↑ Back to the top


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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

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 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:
  1. Start Excel.
  2. In a new workbook, right-click any sheet tab.
  3. On the shortcut menu, click Insert.
  4. In the Insert dialog box, click MS Excel 5.0 Dialog, and then click OK.
  5. Click the Edit Box tool on the Forms toolbar, and then place an edit box on the dialog sheet.
  6. Click the Spinner tool on the Forms toolbar, and then place a spinner on the dialog sheet.
  7. Press ALT+F11 to start the Visual Basic Editor.
  8. On the Insert menu, click Module.
  9. 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
    					
  10. Press ALT+F11 to return to Excel.
  11. Click to select the dialog sheet.
  12. Right-click the spinner control on the dialog sheet, and then click Assign Macro on the shortcut menu.
  13. Click the SpinDate macro, and then click OK.
  14. On the Tools menu, point to Macro, and then click Macros.
  15. 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.

↑ Back to the top


Keywords: KB213464, kbprogramming, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213464
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 320