Update Time Automatically

kumara_faith

Well-known Member
Joined
Aug 19, 2006
Messages
953
Office Version
  1. 365
Hi,

Is is possible to have Excel update time automatically in a given cell within the spreadheet.

I have a spreadsheet that I need Excel to update the time automatically .

I would like Excel to update the time automatically in cell A1.

I have done some research and found the following thread but this only works when we close and open the spreadsheet.

http://www.mrexcel.com/archive2/70800/82079.htm

Is there a way to have Excel update the time without having to close the spreadsheet ?

Thanks.
 
Hi,

This time it works but when I close the workbook, the workbook closes and immediate I get the macro message that we will get if I open the workbook (Enable macro or Disable macro message).

If a choose enable macro, it automatically opens the workbook again. If a choose disable macro, the same thing happens.

Appreciate your guidance and again thank you for your patience.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The only way to get around that is to lower your macro security settings. It isn't possible to do that programatically, for obvious reasons.
 
Upvote 0
Hi,

I went to Tools>>Macro>>Security and selcted "Low" .

Now, when I close the workbook, it doesnt prompt the message but intead opens the workbook again.

Is there a workaround this ? Thanks.
 
Upvote 0
That suggests that the StopClock sub isn't working and I don't know why (as it clearly was before). Are you sure that you have the Workbook_BeforeClose event in the ThisWorkbook's code module.

As a workaround you can run the StopClock sub before closing the workbook.
 
Upvote 0
Hi,

Just to ensure, here is the current code I am using:

On Module 1
Code:
Sub TickTock()
ThisWorkbook.Sheets("Sheet2(POI_Simulation_Test").Range("A1").Value = Now()
NextTick = Now + TimeValue("00:00:01")
Application.OnTime NextTick, "TickTock"
End Sub

Sub StopClock()
On Error Resume Next
Application.OnTime earliesttime:=NextTick, procedure:="TickTock", schedule:=False
On Error GoTo 0
End Sub

On ThisWorkbook
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopClock
End Sub

Private Sub Workbook_Open()
Call TickTock
End Sub

How do I run the StopClock sub before closing the workbook ?

Thanks.
 
Last edited:
Upvote 0
You have a typo here

Code:
SThisWorkbook.Sheets("Sheet(POI_Simulation_Test").Range("C1").Value = Now()

which should be

Code:
ThisWorkbook.Sheets("Sheet(POI_Simulation_Test").Range("C1").Value = Now()

To run StopClock Tools > Macro > Macros, highlight StopClock and click Run.
 
Upvote 0
Hi,

The following is what I am using now:

Code:
Sub TickTock()
ThisWorkbook.Sheets("Sheet(POI_Simulation_Test").Range("C1").Value = Now()
NextTick = Now + TimeValue("00:00:01")
Application.OnTime NextTick, "TickTock"
End Sub

Sub StopClock()
On Error Resume Next
Application.OnTime earliesttime:=NextTick, procedure:="TickTock", schedule:=False
On Error GoTo 0
End Sub

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopClock
End Sub

Private Sub Workbook_Open()
Call TickTock
End Sub

And then I went to Tools > Macro > Macros, highlight StopClock and click Run.

When I open the workbook, I get the following message:

Run Time Error 9
Subscript Out Of Range

When I debug, the following is highlighted:

Code:
ThisWorkbook.Sheets("Sheet(POI_Simulation_Test").Range("C1").Value = Now()

My apologies for all the inconvenience. Appreciate your patience in this one.
 
Upvote 0
As I said before, this is wrong

Code:
ThisWorkbook.Sheets("Sheet(POI_Simulation_Test").Range("C1").Value = Now()

It should be

Code:
ThisWorkbook.Sheets("POI_Simulation_Test").Range("C1").Value = Now()
 
Upvote 0
Hi,

My apologies. I have modified the codes as below:

Module 1
Code:
Sub TickTock()
ThisWorkbook.Sheets("POI_Simulation_Test").Range("C1").Value = Now()
NextTick = Now + TimeValue("00:00:01")
Application.OnTime NextTick, "TickTock"
End Sub

Sub StopClock()
On Error Resume Next
Application.OnTime earliesttime:=NextTick, procedure:="TickTock", schedule:=False
On Error GoTo 0
End Sub

ThisWorkbook
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopClock
End Sub

Private Sub Workbook_Open()
Call TickTock
End Sub

And then I went to Tools > Macro > Macros, highlight StopClock and click Run.

I have also set the macro security to Low.

When I close the workbook and open it again, it works.

However when I want to close it , it doesn't prompt any message but reopen or goes back to the spreadsheet.

Again, I am sorry for taking up much of your time. Appreciate your tolerance and patience.
 
Upvote 0
I can only suggest that you close Excel then re-open your workbook.

If that doesn't work then I'm stumped.
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top