Lesson#185: How to make a digital clock with VBA

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.

  1. 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.

    See also  Lesson#230: How to Recover Unsaved Excel Macros

    Hi! I am Puspendu. I am the founder and author of Excelabcd. I am little creative person, blogger and Excel-maniac guy. I hope you enjoy my blog.

    1 Comment on “Lesson#185: How to make a digital clock with VBA

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *