MS Excel: HOUR Function (WS, VBA)
In Microsoft Excel, the HOUR function returns the hour of a time value (from 0 to 23).
The syntax for the HOUR function is:
Hour( serial_number )
serial_number is the time value to extract the hour from. It may be expressed as a string value, a decimal number, or the result of a formula.
- Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
- Worksheet function (WS)
- VBA function (VBA)
Worksheet Function Example
Let's take a look at an example to see how you would use the HOUR function in a worksheet:
Based on the spreadsheet above, the HOUR function would return the following values:
|=HOUR(A1)||would return 13|
|=HOUR(A2)||would return 7|
|=HOUR("4:30:00 AM")||would return 4|
|=HOUR("4:30:00 PM")||would return 16|
VBA Function Example
The HOUR function can also be used in VBA code. For example:
Dim LHour As Integer LHour = Hour("10:42:58 PM")
In this example, the variable called LHour would now contain the value of 22.