Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Determining the Day of the Month.
Written by Allen Wyatt (last updated February 13, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
When creating macros in VBA, you may have a need to know the specific day of the month represented by a particular date. For instance, you may want to determine the day of the month on which the macro is being executed. The following code will do the trick:
iDay = Day(Date)
The Day function returns an integer value representing the day of the month of whatever date you provide. In this example, the Date function represents today's date, and so Day returns today's day of the month.
You can also, if you prefer, use the Format function to return a text string that contains the day of the month. For instance, consider this code:
sTemp = Format(Date,"dd")
This returns the day of the month as two digits with a leading zero. You could replace "dd" with other variations; "d" returns the date without a leading zero, "ddd" returns the short day name for the day of the month (as in "Wed" or "Fri"), and "dddd" returns the full day name (as in "Wednesday" or "Friday").
Note, however, that Format returns a string. If your subsequent computations require a numeric value, then it is best to use the Day function.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9640) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Determining the Day of the Month.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...
Discover MoreStrings are used quite frequently in macros. You may want to swap the contents of two string variables, and you can do so ...
Discover MoreExcel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-02-13 09:10:38
Brian P
You refer to “day of the month” in this tip, but in places (“ddd” & “dddd”) it appears to be talking about “day of the week” (e.g., Monday, Tuesday, Wednesday). “Day of the month” would refer to the number (1-31).
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments