display current time on Worksheet Activate

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
how can I get (with VBA) the current time in cell A2, and have it updated each time the sheet is activated ?

Thanks.
 

Attachments

  • current_time.png
    current_time.png
    117.9 KB · Views: 3

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If this is to happen on one sheet then try the below inside the sheet module:
VBA Code:
Private Sub Worksheet_Activate()
    Range("A2") = TimeValue(Now())
End Sub

If it is to happen on all sheets then try the below inside the workbook module:
VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Range("A2") = TimeValue(Now())
End Sub
 
Upvote 0
Thanks Georgiboy. Would it also be possible to adapt your code, so that the FORMULA shows in A2 ?

Like range("A2").formula = ...
 
Upvote 0
Yes but if it is a formula, the time will update whenever the sheet is calculated, would that be an issue?
 
Upvote 0
You could just have the formula:

=NOW()

In cell A2 and format it as time?
 
Upvote 0
updating whenever the sheet is calculated is not an issue, so feel free. I have to rush to a meeting now, so take your time ;)
 
Upvote 0
Maybe the below:
VBA Code:
Private Sub Worksheet_Activate()
    Range("A2").Formula = "=--TEXT(NOW(),""hh:mm:ss"")"
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Range("A2").Formula = "=--TEXT(NOW(),""hh:mm:ss"")"
End Sub
 
Upvote 0
Solution
Thanks Georgiboy ! Works perfectly.

I just wonder why the two "--" at the beginning of your formula.
 
Upvote 0
They convert the text result into a numerical value. In the same way the VALUE() function works. If you remove them you will still get a time but as text in the cell.
 
Upvote 0

Forum statistics

Threads
1,216,188
Messages
6,129,401
Members
449,508
Latest member
futureskillsacademy

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