11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

694<br />

Part VI: Developing Custom Worksheet Functions<br />

Date Functions<br />

Chapter 6 presents a number of useful Excel functions and formulas for calculating dates, times,<br />

and time periods by manipulating date and time serial values. This section presents additional<br />

functions that deal with dates.<br />

The companion CD-ROM contains a workbook, date functions.xlsm, that demonstrates<br />

the functions presented in this section.<br />

Calculating the next Monday<br />

The following NEXTMONDAY function accepts a date argument and returns the date of the following<br />

Monday:<br />

Function NEXTMONDAY(d As Date) As Date<br />

NEXTMONDAY = d + 8 – WeekDay(d, vbMonday)<br />

End Function<br />

This function uses the VBA WeekDay function, which returns an integer that represents the day<br />

of the week for a date (1 = Sunday, 2 = Monday, and so on). It also uses a predefined constant,<br />

vbMonday.<br />

The following formula returns 12/27/2010, which is the first Monday after Christmas Day, 2010<br />

(which is a Saturday):<br />

=NEXTMONDAY(DATE(2010,12,25))<br />

The function returns a date serial number. You will need to change the number format<br />

of the cell to display this serial number as an actual date.<br />

If the argument passed to the NEXTMONDAY function is a Monday, the function returns the following<br />

Monday. If you prefer the function to return the same Monday, use this modified version:<br />

Function NEXTMONDAY2(d As Date) As Date<br />

If WeekDay(d) = vbMonday Then<br />

NEXTMONDAY2 = d<br />

Else<br />

NEXTMONDAY2 = d + 8 – WeekDay(d, vbMonday)<br />

End If<br />

End Function

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!