Hello Guys,
Hope You all are fine.
I have come up today with a Count Down timer in excel, prepared with the use of a very simple excel function "Now()"
"Now " function returns the current data and time from the local machine.
Lets first talk about the image you see above.
There is a count down timer above, which shows the count down of specific time set in "Select Cycle time".
Currently, cycle time of 8 Seconds have been set up . When we press Start button, the timer will start and will show a count down time starting from 8 seconds.
Cycle timer is dynamic and user can set the initial time from which the count down will begin.
From the Stop button, user can stop the count down timer before it goes to Zero and can reset the timer to zero with the help of Reset button.
Here is the code for above count down timer in Excel VBA. You can also download this excel file in the link given in the end of this blog.
Option Explicit
Sub StartCountdown()
'----------------------Variable declaration----------------
Dim ZeroHour As Date
Dim TimeDifference As Single
Dim wks_timer As Worksheet
Dim wks_temp As Worksheet
'---------------------Variable initialisation-------------------
Set wks_timer = ThisWorkbook.Sheets("Timer")
Set wks_temp = ThisWorkbook.Sheets("temp")
ResetCountdown
'---------------------Set control value to Zero--------------
wks_temp.Range("B1").Value = 0
'------------------Set start Time as Cycle time--------------
ZeroHour = wks_temp.Range("b2").Value ' Set start time.
TimeDifference = ZeroHour - Now
If TimeDifference <= 0 Then
wks_timer.Range("f15").Value = "Error : date/time in the past"
StopCountdown
Exit Sub
End If
Do While wks_temp.Range("B1").Value = 0 And TimeDifference > 0
DoEvents ' Yield to other processes.
TimeDifference = ZeroHour - Now
wks_timer.Range("f15").Value = Format((TimeDifference), "hh:mm:ss")
Loop
End Sub
Sub StopCountdown()
Dim wks_temp As Worksheet
'Set the control cell to 0 and make it green
Set wks_temp = ThisWorkbook.Sheets("temp")
'Set the control cell to 1
wks_temp.Range("b1").Value = 1
Exit Sub
End Sub
Sub ResetCountdown()
Dim wks_timer As Worksheet
'Set the control cell to 0 and make it green
Set wks_timer = ThisWorkbook.Sheets("timer")
If wks_timer.Range("f15").Value <> 0 Then
'Set the control cell to 1
wks_timer.Range("f15").Value = "0"
End If
End Sub
Click here to download the excel file.
If you like this article or have any query/suggestions, please share your feedback in the comments section below.
If you want to develop any macro for any office product or automate your task in excel or any other MS office product you can reach out to my Fiverr account:
https://www.fiverr.com/users/sg_2021/seller_dashboard
0 comments:
Post a Comment