Click here to Skip to main content
15,029,501 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I prepared a digital clock in Excel VBA, showing each second, every minute and of course every hour.
I have 2 buttons on my worksheet:
- 1 to start the clock (timer is set to update every second)
- 1 to stop the clock
The first button, to start the clock, works perfectly.
The second button, to stop the clock, causes an error.

How can I prevent this error?

Here is the code that I'm using:
Option Explicit

Dim rngHrs As Range
Dim rngMin As Range
Dim rngSec As Range

Sub ClearSec()
    Set rngSec = Range("B1:M5")
End Sub

Sub ClearMin()
    Set rngMin = Range("B7:M11")
End Sub

Sub ClearHrs()
    Set rngHrs = Range("B13:M14")
End Sub

Sub KadersVullen()
    Dim r As Integer
    Dim strTime As Double
    Dim endTime As Double
    Dim wrkTime As Double
    Dim bytSec As Byte
    Dim bytMin As Byte
    Dim bytHrs As Byte
    strTime = CDbl(Now())
    Do While Timer
        bytSec = Second(Time())
        bytMin = Minute(Time())
        bytHrs = Hour(Time())
        'Fill Frame Seconds
        Select Case bytSec
            Case 0
            Case 1 To 12
                r = 1
                Cells(r, bytSec + 1) = bytSec
            Case 13 To 24
                r = 2
                Cells(r, bytSec + 1 - 12) = bytSec
            Case 25 To 36
                r = 3
                Cells(r, bytSec + 1 - 24) = bytSec
            Case 37 To 48
                r = 4
                Cells(r, bytSec + 1 - 36) = bytSec
            Case 49 To 59
                r = 5
                Cells(r, bytSec + 1 - 48) = bytSec
        End Select
        'Fill Frame Minutes
        Select Case bytMin
            Case 0
            Case 1 To 12
                r = 7
                Cells(r, bytMin + 1) = bytMin
            Case 13 To 24
                r = 8
                Cells(r, bytMin + 1 - 12) = bytMin
            Case 25 To 36
                r = 9
                Cells(r, bytMin + 1 - 24) = bytMin
            Case 37 To 48
                r = 10
                Cells(r, bytMin + 1 - 36) = bytMin
            Case 49 To 59
                r = 11
                Cells(r, bytMin + 1 - 48) = bytMin
        End Select
        'Fill Frame Hours
        Select Case bytHrs
            Case 0
            Case 1 To 12
                r = 13
                Cells(r, bytHrs + 1) = bytHrs
            Case 13 To 24
                r = 14
                Cells(r, bytHrs + 1 - 12) = bytHrs
        End Select
'    endTime = CDbl(Now())
'    wrkTime = endTime - strTime

End Sub

Private Sub btnEnd_Click()
End Sub

Private Sub btnStart_Click()
    KadersVullen            'Fill frames
End Sub

What I have tried:

I have no idea where to start my search
Updated 20-Jun-21 22:41pm
Patrice T 17-Jun-21 20:06pm
"How can I prevent this error?"
Apply secret correction to secret error in your secret code.
Herman Broeckx 20-Jun-21 23:28pm
Secret correction didn't help
Dave Kreskowiak 17-Jun-21 20:07pm
And you expect us to guess what you did wrong without you showing the code that is supposed to execute from the Stop button, nor the error message?

We can only work with what you type into these little textboxes. Don't expect people to read your mind.
Herman Broeckx 20-Jun-21 23:27pm
No need to get angry about it
Dave Kreskowiak 20-Jun-21 23:44pm
I have not yet begun to be angry, not will I.

I'm just left with the wonder of how people think providing no useful contextual information for their problems socially function in the real world.

I mean, you walked up to a group of complete strangers, who know nothing about you or what you're working on, and just pointed at parking lot full of cars and said "my car is broken."

Seeing that multiple times day really drags on you.

Since we can't see your code, have no idea what the error message is, or have any other information other than "it doesn't work" we can't help you fix this in any direct way. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And you haven't told us anything!

So it's going to be up to you, and that means you learning a new skill. Let's start at the beginning: Compiling does not mean your code is right! :laugh:

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
Private Function DoubleIt(ByVal value As Integer) As Integer
    Return value * value
End Function

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.

If you have no idea how to use the debugger, this will help:[^]
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
Private Sub btnEnd_Click()
End Sub

Where is the Stop subroutine?
Herman Broeckx 21-Jun-21 10:12am
I was convinced that Stop would be sufficient.
Haven't found helpfull information on Stop yet.
Thank you. Now I know why it is not ending.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900