Page 125 - Excel Progamming Weekend Crash Course
P. 125

h540629 ch08.qxd  9/2/03  9:33 AM  Page 100




                100                                                         Saturday Morning


                   Interval: A string expression specifying the interval being added. See Table 8-1 for
                   the permitted values for this argument.
                   Number: The number of intervals being added. Use a negative value to subtract.
                   Date: The original date.


               Table 8-1 Values for the Interval Argument to the DateAdd Function
               Argument                     Interval
               yyyy                         Year
               q                            Quarter (3 months)

               m                            Month
               y, w, or d                   Day of year
               ww                           Week

               h                            Hour
               n                            Minute
               s                            Second


                  Here are some examples of using the DateAdd function:
                  DateAdd(“q”, 2, Date)             ‘ The date 2 quarters from today.
                  DateAdd(“d”, -60, #12/25/2003#)   ‘ The date 60 days before Christmas 2003
                  DateAdd(“n”, 1000, Time)          ‘ The date (time) 1000 minutes from now.

                  The DateAdd function always returns a valid date and takes leap years into account.
                          If you need to work directly with date serial numbers, use the Single data
                          type. You can assign a date value directly to a type Single variable, and vice
                   Note   versa. For example:
                          Dim d As Date
                          Dim s As Single
                          s = #1/1/2002#        ‘ s now equals 37257, the number of days between
                                             ‘ December 30, 1899 and January 1, 2002.
                          d = s                 ‘ d now contains the date value for 1/1/2002.
                          If you are working only with date values that contain no time part, you can
                          also use the Long data type.



               Date and Time Details

               Once you have a date, how can you obtain details about that date, such as the day of the
               week it falls on? You can use VBA’s DatePart and DateDiff functions for this purpose.
   120   121   122   123   124   125   126   127   128   129   130