VBA - Stopwatch

Copper Contributor

Hi Everyone, 

 

I'm looking for a solution to prevent VBA code from continuing to run when I click on another cell. It should stop the execution of the first cell that was running. Take a look at the VBA code below:

 

 

 

 

Sub startTimer()

'[Top] - Code to Prevent Macro Execution from being displayed on the screen.'
Application.ScreenUpdating = False = Application.Calculation = xlCalculationManual

'Execute Start Counter'
Application.OnTime Now + TimeValue("00:00:01"), "Increment_count"

'[Bottom] - Code to Prevent Macro Execution from being displayed on the screen.'
Application.ScreenUpdating = True = Application.Calculation = xlCalculationAutomatic

End Sub
Sub Increment_count()

'[Top] - Code to Prevent Macro Execution from being displayed on the screen.'
Application.ScreenUpdating = False = Application.Calculation = xlCalculationManual


'Inserir o ActiveCell.Value'
'Range("A3").Value = Range("A3") + 1'
'ActiveCell.Value = ActiveCell.Value2 + 1'


ActiveCell.Value = ActiveCell.Value2 + 1

startTimer

Application.ScreenUpdating = True = Application.Calculation = xlCalculationAutomatic

End Sub

Sub stopTimer()
Application.ScreenUpdating = False = Application.Calculation = xlCalculationManual
Application.OnTime Now + TimeValue("00:00:01"), "Increment_count", Schedule:=False
Application.ScreenUpdating = True = Application.Calculation = xlCalculationAutomatic
End Sub

 

1 Reply

@naesio_maia

Right-click the sheet tab.

Select 'View Code' from the context menu.

Copy the following code into the worksheet module:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    stopTimer
End Sub