WD: How to Determine the Number of Days in a Specified Month

Last reviewed: July 30, 1997
Article ID: Q105536
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a

SUMMARY

To determine the number of days in a specified month, use the following Microsoft WordBasic macro Select Case structure.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

   Sub Main
      'This macro determines the number of days in a given month.
      datev = DateValue(InputBox$("Enter a date in the mm/dd/yy format"))
      mon = Month(datev)
      Select Case mon
         Case 1, 3, 5, 7, 8, 10, 12
            MsgBox "31 days"
         Case 4, 6, 9, 11
            MsgBox "30 days"
         Case 2
            mon = Month(datev)
            If Year(datev) Mod 4 = 0 Then
               MsgBox "There are 29 days in this month"
            Else
               MsgBox "There are 28 days in this month"
            End if
      End Select
   End Sub

This macro prompts for a date in the mm/dd/yy format and returns the number of days in the specified month. The Month function is used to determine the month number for the specified date. Then depending on the month number (1-12), a message box posts the number of days in the month.

The macro takes leap years into consideration when computing the number of days in the month of February.

   If Year(datev) Mod 4 = 0 Then

The year is divided by 4 and if the remainder is 0, the number of days in the month is 29 days.


Additional query words: month day datevalue
Keywords : kbmacroexample winword word6 word7 word95 kbmacro
Version : 6.0 6.0a 6.0c 7.0 7.0a
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.