The VBA Hour Function is a built-in function in MS Excel. It has one input argument or parameter. It returns the two-digit number as Hour from the specified time. It returns a number from 00 to 23 (It has a leading zero where applicable). It is a ‘Data and Hour’ type function. Default format of the Hour function is ‘HH’. The VBA Hour Function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA Hour Function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Hour function, where we can use this VBA Hour Function and real-time examples.
Table of Contents:
- Objective of Hour Function
- Syntax of VBA Hour Function
- Parameters or Arguments
- Where we can apply or use the VBA Hour Function?
- Example 1: Display current time in hours on the screen
- Example 2: Display current time in Hours on the Worksheet
- Example 3: Display Long Time on the screen using Format
- Download File
The syntax of the VBA Hour Function is
There is one parameter or argument for the Hour function in VBA. That is nothing but ‘time_value’.
Where time_value is a time to generate number as hour from the specified time. It returns a number from 00 to 23. It has leading zero where applicable.
We can use this VBA Hour function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Here is a simple example of the VBA Hour Function. This below example macro uses the Hour function and displays the two-digit Hour from the specified date. In this case specified time is current time.
'Display Current Hour on the screen Sub VBA_Hour_Function_Example1() 'Variable declaration Dim sCurrentHour As Integer 'Assign current Hour to variable sCurrentHour = Hour(Time) MsgBox sCurrentHour, vbInformation, "Current Hour" End Sub
In the above example ‘sCurrentHour’ declared as an Integer data type. And Hour is a vba function, it returns two digit number (number from a 0 to 23) as Hour. This variable ‘sCurrentHour’ now contains the two-digit Hour.
Here is the screen shot of first example output.
Here is another example of the VBA Hour Function. This below example macro uses the Hour function and displays the Hour from the specified time on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.
Sub VBA_Hour_Function_ Example2() 'Display Current Hour on the Worksheet 'Variable declaration Dim dCurrentHour As Date 'Assign current Hour to variable sCurrentHour = Now Sheets("VBAF1.com").Range("B18") = Hour(sCurrentHour) 'or 'Sheets("VBAF1.com").Cells(18, 2) = Hour(sCurrentHour) End Sub
Here is the screen shot of second example output.
Here is one more example with VBA Hour Function. This below example macro uses the Time function and changes the format of the time. Finally, it displays the long time from the specified time on the screen. Long time includes hours, minutes and seconds.
Sub VBA_Hour_Function_ Example3() 'Format and Display Long Time on the screen 'Variable declaration Dim sCurrentHour As Date 'Assign current Hour to variable sCurrentHour = Time MsgBox Format(sCurrentHour, "Long Time"), vbInformation, "Current Long Time" End Sub
Here is the screen shot of third example output.
Here are the instructions to use the VBA Hour function in Excel
- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Insert above specified macro example.
- Select code and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
- Ex1-Output: It displays two-digit Hour on the screen.
- Ex2-Output: It displays two-digit Hour on the Worksheet in Rage B18.
- Ex3-Output: It displays long time form the specified time on the screen.
- Note: Difference between first output and second output is format of the Hour.
Click on following link to download free example excel workbook to learn more about the VBA Hour function.
List of VBA Functions:
Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions