6
I am working on a spreadsheet where the user has to type a given text into a cell at a certain time. I want to put a countdown side, so he can check how much time is left. However, when I start typing in the cell, the stopwatch function stops working (stops counting), and only comes back after I give Enter. I’m new in VBA, maybe it’s some configuration, or where I’m running the subroutines, and I don’t know yet.
The regressive counter routines are these:
Private Sub Iniciar_crono()
starttimer
End Sub
Sub starttimer()
Application.OnTime Now + TimeValue("00:00:01"), "nexttick"
End Sub
Sub nexttick()
If Plan3.Range("E3") = 0 Then
Exit Sub
End If
Plan3.Range("E3").Value = Plan3.Range("E3").Value - TimeValue("00:00:01")
If Plan3.Range("E3").Value <= TimeValue("00:00:10") Then
Plan2.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
Plan2.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(255, 255, 255)
End If
starttimer
End Sub
Private Sub Parar_crono()
stoptimer
End Sub
Sub stoptimer()
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:01"), "nexttick", , False
End Sub
For guidance on: - A button to start the counter calls Start_chrono() - A button to stop it calls Parar_crono() - Everything is now run in Module 1
As the colleague @Cantoni already put well in his reply, Excel blocks the execution of the timer while in edit mode. I went online to see if you could do that with native Windows timer functions. And it is, because unlike the VBA, they are not blocked. Only, even so, you will incur another problem: Excel will probably crash if you try to change a cell value while it is in edit mode. :/
– Luiz Vieira
Okay, I get what I’m doing wrong. I’ll try other means. An option I thought is to fill a Floating Text Box instead of a cell. I did a test, and it does not stop the execution of VBA code. I didn’t want to do much outside Excel, because I’m making this application mainly to learn VBA, so the bigger the challenge the better! =)
– FLemos
If it’s just to learn, then you’ve already learned something. ;) But, an alternative to do what you want is to use a text field in a user form (more information here: http://www.macoratti.net/12/vba_lgn1.htm). Thus, you have full control over what the user can type, or how long it will take for that. And, you can transfer the data from there to the spreadsheet cells (which may be blocked for editing).
– Luiz Vieira