Calculate Difference Between two Times in Excel VBA

Calculate Difference Between two Times

Calculate Difference Between two Times in Excel VBA. Most of the time we wanted find differences of two Times. It’s a very regular and common exercise in Excel and VBA when dealing with times. We can find this using Excel VBA DateDiff function. In order to calculate we need start time and end time. It returns an integer value as a result.

Macro to Calculate Hours Between two Times in Excel VBA

Let us see the macro to calculate Difference Between two Times in Excel VBA.

'Calculate Difference Between two Times in Excel VBA
Sub VBA_Calculate_Difference_Between_Two_Times()
    
    'Variable declaration
    Dim sDate As Date, eDate As Date
    Dim iValue As Integer
    
    'Start Date
    sDate = "8:10:00"
    
    'End Date
    eDate = "12:10:00"
    
    iValue = DateDiff("h", sDate, eDate)
    
    'Display output on the screen
    MsgBox "The Difference Between two Times in hours : " & iValue, vbInformation, "Difference Between two Times in Hours"
    
End Sub

Here is the output screenshot of above macro.
Calculate Difference Between two Times

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment