Equipment rolling run hours help

seanb12491

New Member
Joined
Feb 23, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello. I'm fairly new to vba but I've managed to get a macro driven sheet together to help track run hours on a equipment category. Currently I have 3 columns assigned to a time format with the format as [hh]:mm. The 3 colums are start time, end time, and the difference.

What i am attempting to do is using vba copy the end time to a variable, then save the variable as the new start time.

But when I seems to stick at 24:00. Currently the cells show time as 9:00 PM and after the vba runs it shows time as 1/1/1900 00:00am

Any idea what I'm doing wrong with the formating
 
Hi, could you post your formulas or code that leads to this error?

Calculating times in Excel can be tricky and the results sometimes hard to predict.
 
Upvote 0
You cannot assign a time to a variable, rather a date.
But if you need to store a time in a variable it has to be of a different format, a time value actually.
Besides, calculating time differences can lead to wrong results if, for example, the time period goes past midnight.
So you need to handle this too.
If you simply copy & paste the time from a Sheet to VBA it will be simply text or a so called "String".

Here's an example of what you could do instead:

MrExcel_2025-02.xlsm
ABC
1start timeend timediff
29:00 AM5:00 PM8:00:00
39:00 PM6:00 AM9:00:00
Sheet1
Cell Formulas
RangeFormula
C2:C3C2= TimeDiff(A2,B2)


VBA Code:
Public Function TimeDiff(a As Date, b As Date) As Date
   If (a < b) Then
      TimeDiff = b - a
   Else
      b = DateAdd("d", 1, b)
      TimeDiff = b - a
   End If
End Function

In order to work from within a Sheet the VBA code has to be placed in a module!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top