PC Review


Reply
Thread Tools Rate Thread

Adding Time to a variable in Excel VBA

 
 
Gum
Guest
Posts: n/a
 
      22nd Dec 2008
I have a time string:
mytime= Format(Now(), "hh:mm:ss AMPM")
I need to add "00:00:40" to mytime
How can I do so and avoid mismatching.
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      22nd Dec 2008
t = Now() + TimeValue("00:00:40")
mytime = Format(t, "hh:mm:ss AMPM")

--
Gary''s Student - gsnu200822


"Gum" wrote:

> I have a time string:
> mytime= Format(Now(), "hh:mm:ss AMPM")
> I need to add "00:00:40" to mytime
> How can I do so and avoid mismatching.

 
Reply With Quote
 
RadarEye
Guest
Posts: n/a
 
      22nd Dec 2008
Hi Gum,

In your sample mytime is a string variable.
So you can not add any DateTime value to it.
This has to be done before the Format function is used.

Gary's version works fine but you cab also use:

mtyime = Format(DateAdd("s", 40, Now()), "hh:mm:ss AMPM")

HTH,

Wouter
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      22nd Dec 2008
The Format function produces a human readable string of text (in Variant
form), not a date/time. Dates and times in VB are actually stored in as a
Double... the whole number part represents the number of days since "date
zero" (which is 12/30/1899) and the decimal part represents the fraction of
a 24-hour day; so formatting the Now function does nothing to aid you in
your goal. VB provides a rich set of Date manipulation functions which you
might want to familiarize yourself with in the help files (use "date
functions", with the quotes, when searching the VBA editor's help files).
Now, to answer your question, I would use this...

mytime = DateAdd("s", 40, Now)

--
Rick (MVP - Excel)


"Gum" <(E-Mail Removed)> wrote in message
news:63A60B96-86D5-4879-A28E-(E-Mail Removed)...
>I have a time string:
> mytime= Format(Now(), "hh:mm:ss AMPM")
> I need to add "00:00:40" to mytime
> How can I do so and avoid mismatching.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding time in excel rodchar Microsoft Excel Misc 2 17th Dec 2008 09:10 PM
Adding a string time value to a date variable John Dann Microsoft VB .NET 2 3rd Feb 2007 09:42 PM
Adding time in Excel =?Utf-8?B?TW92aWVTY2hlZHVsZXM=?= Microsoft Excel Worksheet Functions 1 31st Jan 2005 10:18 AM
Adding time in Excel =?Utf-8?B?dGFoaXJhYg==?= Microsoft Excel Worksheet Functions 1 10th Nov 2004 09:47 PM
Adding time in excel Juan Microsoft Excel Misc 1 6th Aug 2004 06:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:55 AM.