Excel VBA Hour Function
The hour function in VBA returns the hour element for the specified time.
Syntax
Hour (Time)
Parameter
Time (required) – This parameter represents the time.
Return
This function returns the hour element for the specified time.
Example 1
Sub HourFunction_Example1() ' Extracting the hour element from the given time Const Time_Val = "6:16:53" Dim hour_val As Integer hour_val = Hour(Time_Val) ' The variable hour_val will return 6. Cells(1, 1).Value = hour_val End Sub
Output
6
Example 2
Sub HourFunction_Example2() ' Extracting the hour element from the given time Dim Time_val Time_val = Time() Dim hour_val As Integer hour_val = Hour(Time_val) ' The variable hour_val will return type mismatch error. Cells(1, 1).Value = hour_val End Sub
Output
22
Example 3
Sub HourFunction_Example3() ' Extracting the hour element from the given time Dim Time_val Time_val = "Hello" Dim hour_val As Integer hour_val = Hour(Time_val) ' The variable hour_val will return type mismatch error. Cells(1, 1).Value = hour_val End Sub
Output