Lesson#185: How to make a digital clock with VBA
In this post, we shall learn a simple to make a digital clock with VBA on your excel sheet.
- Open the VBA editor: In Excel, press Alt + F11 to open the VBA editor.
- Create a new module: In the VBA editor, go to the Insert menu and select Module. This will create a new module where you can write your VBA code.
Now paste this VBA macro that you can use to create a digital clock on your worksheet,
Sub CreateDigitalClock()
' Set up the clock timer
Application.OnTime Now + TimeValue("00:00:01"), "UpdateDigitalClock"
End Sub
Sub UpdateDigitalClock()
' Get the current time
Dim currentTime As Date
currentTime = Time
' Update the clock display
Range("A1").Value = Format(currentTime, "hh:mm:ss")
' Set up the clock timer to run again in one second
Application.OnTime Now + TimeValue("00:00:01"), "UpdateDigitalClock"
End Sub
To use this macro, place it in a module in your VBA project and run the CreateDigitalClock
macro. This will set up a timer that will run the UpdateDigitalClock
macro every second to update the clock display in cell A1 with the current time.
Absolutely right think of Digital Clock