VBA HOUR Function

VBA HOUR Function

Objective of the VBA Hour Function:

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:

Syntax of VBA Hour Function

The syntax of the VBA Hour Function is

HOUR(time_value)

Parameters or Arguments

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.

Where we can apply or use the VBA Hour Function?

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.

Example 1: Display current Hour on the screen

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.
Output:
Here is the screen shot of first example output.
VBA HOUR Function Example 1

 

Example 2: Display current Hour on the Worksheet

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
    

Output:
Here is the screen shot of second example output.
VBA HOUR Function Example 2

 

Example 3: Display Long Time on the screen using Format

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

Output:
Here is the screen shot of third example output.
VBA HOUR Function Example 3

Instructions

Here are the instructions to use the VBA Hour function in Excel

  1. Open an Excel workbook.
  2. Press Alt+F11 to open VBA Editor window.
  3. Go to Insert menu and click on module from the available options.
  4. Insert above specified macro example.
  5. Select code and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
  6. Ex1-Output: It displays two-digit Hour on the screen.
  7. Ex2-Output: It displays two-digit Hour on the Worksheet in Rage B18.
  8. Ex3-Output: It displays long time form the specified time on the screen.
  9. Note: Difference between first output and second output is format of the Hour.

Download File

Click on following link to download free example excel workbook to learn more about the VBA Hour function.

VBA Hour Function Examples

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *