Sometimes you find yourself in a difficult situation because your deadline is approaching. So to use your time more efficiently, you can set a digital clock along with your Excel worksheet. How will you do that? Well, no problem at all. Here we will provide a VBA code for a digital clock in Excel and later we will demonstrate how to use the VBA code so that you can create one of your own.
Download Practice Workbook
You can download and practice the dataset that we have used to prepare this article.
2 Steps to Use VBA Code for Making a Digital Clock in Excel
Now, look at the screenshot we have provided below. We will demonstrate in the following section how to create this digital image with adequate aesthetic features. So let’s begin.
Here, we have used the Microsoft Excel 365 version; you may use any other version according to your convenience.
Step 1: Employ VBA Code in Module
The very first step of this process is to incorporate the VBA code into our Excel file. We will apply the VBA TimeValue function within our VBA code so that the code can take data from the inbuilt function to keep the time updated.
- Press Alt + F11 to open your Microsoft Visual Basic.
- Then press Insert > Module to open a blank module.
- Now, 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
- Now, we will explain how the given VBA code works.
⚡ Code Breakdown: The code is divided into three steps.
- Now, press F5 to run your VBA Code. Subsequently, the macro window will appear.
- Click on MakingClock > Run.
- Now see the output of the code, as given below.
Read More: How to Make a Running Clock in Excel (2 Easy Ways)
Step 2: Format Alignment and Color Grading
So far, so good. Now we will make an arrangement to give our clock a better look. Not to mention how important it is to combine art and beauty in our day-to-day lives. Here we make a typical choice of color and presentation. However, you can use anything according to your individual preference.
- First of all, select the B4:E7 cells, then press Merge & Center.
- Following this, move the clock in center alignment by clicking the feature as shown in box 1.
- To make the border, select All Borders from the borders option.
- After that, change the font to Digital 7.
- Increase the font size to 48. You may choose the font size according to your own preference.
- Now we will change the Font Color as well as the background.
- To do so, select the Font Color and Fill Color features as depicted below.
- So, the final result will appear below. Looks cool, right?
Read More: How to Create Analog Clock in Excel (with Easy Steps)
Things to Remember
- To stop your clock, select the Disable option from the Macro You can find this option by going to Developer > Macros > Disable > Run.
Practice Section
We have provided a Practice section on the right side of the sheet so that you can practice yourself.
Conclusion
In this article, we have discussed the application VBA code for creating the digital clock in Excel. It will be fun, we can tell you when you do it yourself. However, if you have any queries, feel free to comment below, and we will get back to you soon.
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
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