View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default trouble with subtracting elapsed times

To NDBC,

As an added tip when working with times that can go over 24 hours, if you
declare date variables (In VBA times use Date variables because there are no
time variables) and all of your times are assigned to the date variables and
they include the date. for instance myTime = Now. myTime includes both Date
and time.

All calculations are then done with the VBA date variables because the date
variable holds both the date and time. That allows you to subtract a time
today from a time tomorrow and get the correct answer which if viewed as date
and time could actually be something like 1/1/1900 10:19:23. Convert this to
a serial number and it will be something like 1.43012731481.

Now if you format that number to a time using the worksheetfunction.text
using the [hh] option then it will display as 34:19:23.

You only convert to the time numberformat with worksheetfunction.text when
assigning the variable to a textbox etc so that it only displays the time
without the date. Never try to then use the textbox value for any
calculations. Always use the original VBA date variables.

You might find that you need to declare the date variables in the
Declarations area before any subs and declare them as public. That way they
will retain their values during the current session and are available for use
in any sub or module.
Example:
Public myDate as Date

Hope this info helps.

--
Regards,

OssieMac