View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default use Excel Now() in VBA

Maybe something like this then -

Sub SplitTime()

With Range("A1")
Range("B1") = .Value
.Formula = "=NOW()"
.Value = .Value
End With

End Sub

Sub NumFormat()
Range("A1:B1").NumberFormat = "hh:mm:ss.00"
End Sub

Regards,
Peter T

"zip22" wrote in message
...
I am putting together a sort of split timer, but seeing the previous value
where the timer was last stopped is useful. Simplified, I have a value in
cell a1 that is the time the stopwatch was last stopped. When I press a
button (run the macro), the split time between a1 and now is entered into
b1.
A1 is reset to now. Press the button again and the split time time is
entered into b2. Press again, and b3 is filled in, etc

Comparing the current time to cell a1 lets the person know the approximate
running split time. It is better to keep this slightly inaccurate, and
not
use a running timer. A running timer may enourage trying to match the
previous split time instead of accurately watching the event. The rough
idea
that the previous end time and the current system time gives is a good
enough
measure.

Accuracy to one hundredth of a second is acceptable.

looking at "timer" in VBA, shouldn't
now and timer match? (For the time portion anyways)

On my system, they currently differ by 0:20:12 and it is drifting higher


"Peter T" wrote:

Generally it's best to avoid square brackets. Difficult to answer your
main
question though until you give some information about what I asked you
previously.

Regards,
Peter T

"zip22" wrote in message
...
[now()] did what I was looking for. I don't think it was a data type
issue.
The cells are set to "mm:ss.00"

range("A1") = Now
always rounds down to the second

range("A1") = [Now()]
gives me hundredths of a second

after looking into the square brackets, it looks like i can also use
[A1]=[Now()]

This looks like it will be more straightforward to code. Is there any
downside to using this instead of timer? (if I am happy with hundredths
of
a
second)



"Peter T" wrote:

What's the purpose, IOW do you want a timer or do you want to know the
actual time, and in either case to what resolution.

FWIW Now() normally gets coerced to one second in cells due to the
Date
type
conversion. However it's actual resolution is to 1/100 sec (at least
in
my
light testing) so maybe simply -
dim x as double
x = [now()]

Despite the timer bug Rick mentioned, I've never had a problem with
it.
So
for quick testing where a resolution of about 1/20 sec is enough I use
VBA's
Timer function. For higher resolution there are various APIs, eg
GetTickCount (that also has a rollover but it's never bit me!).

Regards,
Peter T



"zip22" wrote in message
...
Excel's Now() function has a resolution down to hh:mm:ss.00 where
the
VBA
Now
function only has hh:mm:ss. I need the accuracy of the excel now
function
in
a macro but

application.worksheetfunction.now

does not work. My workaround at this point is referencing a cell
with
"=now()" in it, but before I reference it I have to use
application.calculate
so it updates. Is there a better way to do this?


.



.