Adding Time to a variable in Excel VBA

G

Gum

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.
 
R

RadarEye

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
 
R

Rick Rothstein

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top