Find Duration in Hours between two Dates using VBA

Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010




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! :)
  1. First we need to import the data from the text file into an Excel worksheet
  2. We then write the VBA Code
  3. VBA code: Sub calculateHours()
    Range("C1").Value = (Range("B1").Value - Range("A1").Value) * 24
    End Sub
  4. We subtract the start date from the end date and multiply the result by 24
  5. 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:


Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010