Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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 Hour of the Day.
Written by Allen Wyatt (last updated December 16, 2021)
This tip applies to Excel 2007, 2010, and 2013
If you are writing macros for Excel, you may have a need to determine the hour represented by a particular date and time value. For instance, you might want to know the hour of the day in which the macro is running. You can ascertain this information by using the HOUR function, as follows:
iThisHour = Hour(Now())
When executed, iThisHour will be equal to the current hour number, which ranges from 0 to 23. Notice that this example uses the Now() function. If you want to determine the hour number for a different date and time value, simply substitute that value in place of the Now() function.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9009) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Determining the Hour of the Day.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Figuring out where Windows places certain items (such as the user's desktop) can be a bit frustrating. Fortunately, there ...
Discover MoreOne way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...
Discover MoreIt is not uncommon to set variables in a macro based on other values, such as time or date. You could also set variables ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2013-10-15 10:07:47
Bryan
In VBA you don't need to include the parentheses if there are no arguments, so Hour(Now) is the same as Hour(Now()). There's no harm in leaving them, other than you might be tempted to put in an argument where there is none.
In contrast, in Excel you must use the parentheses, as this is how Excel knows you are referencing a function. =HOUR(NOW()) will give you the correct result, whereas =HOUR(NOW) will display the #NAME? Error.
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