Saturday 1 December 2018

Create a Stopwatch in Excel Using Excel VBA

Step 1: Select a desired range of cells and merge them.


Step 2: Type "00:00:00" to the cell and format it according to the desired mode.


Step 3: Insert command buttons. Go to "Developer" tab, click "Insert" and look for command button under "ActiveX Controls" and label the button as "Start". To label a command button, the guideline is shown under the "Remark" right after Step 4. 



Step 4: Repeat Step 3 by creating and designing another 2 command buttons and label them as "Stop", and "Reset" respectively.


*Remark: To design those buttons, can click "Properties".



Step 5: Type the following codes to Visual Basic.

Private a As Boolean

Private Sub CommandButton1_Click()
a = True
Do While a
Application.Wait (Now + #12:00:01 AM#)
DoEvents
Sheet3.Cells(5, "H") = Format(DateAdd("s", 1, Sheet3.Cells(5, "H")), "hh:mm:ss")
Loop
End Sub

Private Sub CommandButton2_Click()
a = False
End Sub

Private Sub CommandButton3_Click()
Sheet3.Cells(5, "H") = "00:00:00"
End Sub


*Remark:
1. "Do While ... Loop" is to create an infinite repeating process until it is interrupted (where "a" is set to "False").
2. "Application.Wait (Now + #)" creates a pause of # seconds long. As in the example above, #12:00:01 AM# is one second long.
3. "DoEvents" is used to ensure Excel remains responsive, i.e. the change in time is shown (updated) on the select cells. 
4. Sheet#.Cells(#, "#") - all # have to change to the correct numbers or characters.
5. In the "DateAdd" function, "s" is a unit measure for "second". Other unit measures include: "yyyy" for year, "q" for quarter, "m" for month, "y" for day of year, "d" for day, "w" for weekday, "ww" for week, "h" for hour, and "n" for minute. (Source: Microsoft, 2018. DateAdd function. https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function)


If you need a visual or audio aid. kindly watch the below video. Hope it helps. 




10 comments:

  1. Start works fine but Stop won't respond once the timer has started??

    ReplyDelete
  2. My mistake...working now...thanks for the video

    ReplyDelete
    Replies
    1. So sorry that I didn't see your message earlier. I'm glad to hear that everything is fine now.
      Happy working and have a nice day

      Delete
  3. Tengo Excel 2007 y me marca como incorrecto
    Sheet3.Cells(5, "H") = Format(DateAdd("s", 1, Sheet3.Cells(5, "H")), "hh:mm:ss")
    que puedo hacer para solucionarlo

    ReplyDelete
    Replies
    1. Hi. This may because the number "3" in "Sheet3.Cells" is not the sheet number that you were using. You just have to change it to the number that you are using.

      To check the sheet number, go to Visual Basic, under "Project - VBAProject", the numbering is right under the "Microsoft Excel Objects".

      Hope it helps. Have a nice day.

      Delete
    2. Hola. Esto puede deberse a que el número "3" en "Sheet3.Cells" no es el número de hoja que estaba utilizando. Solo tiene que cambiarlo al número que está utilizando.

      Para verificar el número de hoja, vaya a Visual Basic, en "Proyecto - Proyecto VBAP", la numeración está justo debajo de "Objetos de Microsoft Excel".

      Espero eso ayude. Que tengas un buen día.

      Delete
  4. I can't find my Developer Tab.

    I'm useing Office 2007 in windows 7 version

    ReplyDelete
    Replies
    1. Hi. You may try:
      Go to File, look for option, look for Customize Ribbon, then under the Main Tabs, check "Developer" and press "OK".
      You should now see the Developer tab.
      Help it helps. Have a nice day.

      Delete
  5. How do you assign the buttons to the Macros? I followed exactly as you instructed but my button doesn't give me the "assign macro" option when I right click

    ReplyDelete