Find Duration in Hours between two Dates using VBA

Could you help me in finding the duration in hours between two dates using VBA code? I have a range of dates as a text file. I have the start date and the end date. The dates are written as 05-Jan-2012 9:15 and 14-Jan-2012 8:10 for example, and I’m supposed to find the total time in hours between the two dates.
Thanks in advance! 🙂

First we need to import the data from the text file into an Excel worksheet
We then write the VBA Code
VBA code: Sub calculateHours()
Range(“C1”).Value = (Range(“B1”).Value – Range(“A1”).Value) * 24
End Sub
We subtract the start date from the end date and multiply the result by 24
If you had more data in the text file then you would need to use a ‘do while’ or ‘for loop’ to perform all the calculations

Watch the training video below to learn how to find total number of hours between two dates:

2 thoughts on “Find Duration in Hours between two Dates using VBA”

  1. Hello Sir,

    Thank you so much for your videos, as a beginner its helped me a lot.
    I have a question that:
    I have a sheet with multiple rows(variable no.of rows each time) and columns with starting date,ending date and department etc.
    so i would like to split the Data into different sheets based on Department names, with starting and ending date.

    Example: Starting date Ending date Department
    12/12/19 12/12/19 SALES
    12/12/19 19/12/20 MARKETING
    20/12/19 29/12/19 SALES

    If i enter start date 10/12/19 and
    enddate 20/12/19
    the Data should be divided into number of sheets like
    Sheet1 -sales
    sheet2 -marketing
    in given time period.
    cloud you please help me to solve this problem.
    thanks in advance.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.