tags:

views:

1417

answers:

2

How can I make excel continuously calculate a sheet/range in realtime (not 1 calc/sec) and do it in the background?

I want this metric clock to run like a stopwatch....

=IF(LEN(ROUND((HOUR(NOW())*(100/24)),0))=1,"0"&ROUND((HOUR(NOW())*(100/24)),0),ROUND((HOUR(NOW())*(100/24)),0))&":"&IF(LEN(ROUND((MINUTE(NOW())*(100/60)),0))=1,"0"&ROUND((MINUTE(NOW())*(100/60)),0),ROUND((MINUTE(NOW())*(100/60)),0))&":"&IF(LEN(ROUND((SECOND(NOW())*(100/60)),0))=1,"0"&ROUND((SECOND(NOW())*(100/60)),0),ROUND((SECOND(NOW())*(100/60)),0))

+2  A: 

I think this might fail your "(not 1 calc/sec)" criteria, but I achieved something similar as follows. Assumes your formula is in cell A1 of a worksheet named Sheet1.

In the ThisWorkbook code module:

Private Sub Workbook_Open()
    Application.OnTime Now + TimeValue("00:00:01"), "RecalculateRange"
End Sub

... and in a regular code module:

Public Sub RecalculateRange()
    Sheet1.Range("A1").Calculate
    Application.OnTime Now + TimeValue("00:00:01"), "RecalculateRange"
End Sub
Graham Miller
+1  A: 

I've used the following to produce the effect you are looking for:

Option Explicit

Public TimerRunning As Boolean
Dim CalculationDelay As Integer

Public Sub StartStop_Click()
    If (TimerRunning) Then
        TimerRunning = False
    Else
        TimerRunning = True
        TimerLoop
    End If
End Sub

Private Sub TimerLoop()
    Do While TimerRunning
        '// tweak this value to change how often the calculation is performed '
        If (CalculationDelay > 500) Then
            CalculationDelay = 0
            Application.Calculate
        Else
            CalculationDelay = CalculationDelay + 1
        End If
        DoEvents
    Loop
End Sub

StartStop_Click is the macro that I tie to the Start/Stop button for the stopwatch. You can get fancy, and change its name to "Start" or "Stop" depending on the value of TimerRunning, but I kept things simple to illustrate the concept.

The two key things here are:

Application.Calculate

Which forces Excel to calculate the worksheet, and:

DoEvents

Which allows VBA to run in the background (i.e. Excel does not stop responding to user input). This is what allows you to still press the "Stop" Button even though the timer is running.

e.James
this was what i was looking for... thanks... is it possible to recalculate within a second? like maybe 10 times a second? by tricking the now() function somehow? or initializing several separate "now" variables that are offset by a tenth of a second?
CheeseConQueso
also, ive noticed that typing into a cell while the watch is running will pause the output in A1 to change, but will not stop the macro (StartStop_Click) - any way around that?
CheeseConQueso
Re Comment #1: Are you trying to see more resolution on the timer, as in: HH:MM:SS:MSS? The NOW() function should have the same resolution as the system clock (usually about 10 milliseconds), but the update time depends on how much Excel has to do between calls to DoEvents.
e.James
Re Comment #2: What is the behavior you are looking for? Should typing in another cell stop the timer?
e.James
I am looking for more resolution - like the activity that happens when you hold F9 down with this formula in a cell " =TEXT(TEXT(NOW(),"hh:mm:ss.000"),"hh:mm:ss.000") ". And nothing should stop the timer except that button click you made.
CheeseConQueso
if i could get just the milliseconds from the now() function, I could make it work i think
CheeseConQueso
I'm not sure how to prevent text entry from stopping the timer. You may have to pose it as a new question on SO. :)
e.James
As for the improved resolution, why not use the shorter formula =TEXT(TEXT(NOW(),"hh:mm:ss.000"),"hh:mm:ss.000") instead of that long monster you quoted in the original question? Since the VBA code I posted simply calls Application.Calculate, it should have exactly the same effect as holding down F9
e.James
You may also have to reduce the "500" number in the looping code. It slows down the updating process. If you set it to a lower number, the timer will be updated quicker, but Excel will hog more processor time.
e.James
it seems from the smaller formula that i won't be able to calculate anything from the milliseconds... wait, they are already "metric"... ill try to left chop the 3 chars off - thanks for all the help
CheeseConQueso
right chop... slap chop
CheeseConQueso
The result of the now() function is simply a floating-point number representing the number of days since 12:00 AM on Janurary 1, 1900. You can use =NOW()*24*60*60*1000 to convert to milliseconds.
e.James
I'm not sure if that helps you out. When I used Excel to make a stopwatch, I had the StartStop button copy the value of NOW() into a cell (say A1), and then the timer calculation was done as NOW()-A1. This gave the exact time difference between NOW() and the time when the StartStop button was clicked. I was able to show hours, minutes, seconds and milliseconds by using =TEXT(NOW()-A1,"HH:MM:SS.000")
e.James
truth be told - thanks
CheeseConQueso