How to Use VBA Code for Making a Digital Clock in Excel: 2 Steps

Method 1 – Employ VBA Code in Module

  • Press Alt + F11 to open your Microsoft Visual Basic.
  • Press Insert > Module to open a blank module.

Employ VBA Code in Module

  • Write the following VBA code in your Module1.
Dim DigitalClock As Date
Sub MakingClock()
With Sheet1.Range("B4")
.Value = Format(Time, "hh:mm:ss AM/PM")
End With
Call SetTime
End Sub
Sub SetTime()
DigitalClock = Now + TimeValue("00:00:01")
Application.OnTime DigitalClock, "MakingClock"
End Sub
Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=DigitalClock, Procedure:="MakingClock", Schedule:=False
End Sub

Employ VBA Code in Module

  • Explain how the given VBA code works.

 

Code Breakdown:

The code is divided into three steps.

  • A variable named Digitalclock has been created. Later a sub-function MakingClock was assigned to the B4
  • The sub-function SetTime will keep the addition of TimeValue and present value (from when the code will be run) in Digitalclock
  • The user wants to disable his clock, another sub-function Disable() has been created in the last part of the code.

  • Press F5 to run your VBA Code. The macro window will appear.
  • Click on MakingClock > Run.

  • The output of the code is given below.


Method 2 – Format Alignment and Color Grading

  • Select the B4:E7 cells, then press Merge & Center.

  • Move the clock in center alignment by clicking the feature as shown in box 1.
  • Select All Borders from the borders option.

  • Change the font to Digital 7.

  • Increase the font size to 48. You may choose the font size according to your own preference.

How to Use VBA Code for Creating Digital Clock in Excel

  • We will change the Font Color as well as the background.
  • Select the Font Color and Fill Color features as depicted below.

How to Use VBA Code for Creating Digital Clock in Excel

  • The final result will appear below.


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mohammad Shah Miran
Mohammad Shah Miran

Mohammad Shah Miran has a professional background spanning over a year at Softeko. Initially starting as an Excel and VBA writer, he authored more than 50 articles for the ExcelDemy project. Currently, Miran is engaged in the LinuxSimply project as a Linux content developer, completed over 40 articles. His analytical approach extends across various domains, including Excel, VBA, Bash scripting, Linux, data analysis, and Python programming. In his leisure time, Miran enjoys watching movies and series or listening... Read Full Bio

2 Comments
  1. Dear Sir

    I have to convert the MS formula into VBA code can you help me?

    =IF(ISBLANK(E13),””,MAX(D$12:$D12)+1)

    Thanks

    • Reply Avatar photo
      Mohammad Shah Miran Dec 29, 2022 at 1:10 PM

      Hello Imran,
      Thanks for the query. What I understand from your comment is that you want to incorporate a VBA code for the formula instead of going to the formula editor. Here are my two cents which might help you in this regard. Look at the dataset attached below.

      After pressing the ALT+F11 short key to open your VBA window, paste the following code in the Module box.

      Save & Close your VBA window. Then, press F8 to open the Macro dialog box and click on Options.

      Create a shortcut key to make the process fast, Crtl+W for instance.

      Now see the output as given below.

      Hope you have got your answer. Good Luck!

      Regards
      Miran
      Excel & VBA Content Developer

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo