Gostaria de registrar o tempo que leva para terminar de preencher um formulário no Excel. Criei um formulário, onde tenho as seguintes colunas;
start, col1, col2 ..., col8, end, time
Gostaria que o cronômetro iniciasse quando alguém digitasse 1 na coluna inicial e terminasse a gravação quando alguém digitasse 1 na coluna final. O tempo deveria aparecer na coluna de tempo.
Eu escrevi o seguinte código VBA com base em tutoriais online, mas ele não funciona (nada acontece quando eu insiro 1 na primeira coluna da primeira linha (início) e então 1 na primeira linha da última coluna (término), a coluna time permanece vazia). Como o código deve ser ajustado para fazer o que eu preciso que ele faça?
Dim StartTimes As Object ' Dictionary to store start times
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("form")
Dim StartCol As Integer: StartCol = 1 ' "start" column
Dim FinishCol As Integer: FinishCol = 10 ' "finish" column
Dim TimeCol As Integer: TimeCol = 11 ' "time" column
If StartTimes Is Nothing Then Set StartTimes = CreateObject("Scripting.Dictionary")
If Not Intersect(Target, ws.Columns(StartCol)) Is Nothing Then
If Target.Value = 1 Then
StartTimes(Target.Row) = Now ' Store current timestamp
End If
End If
If Not Intersect(Target, ws.Columns(FinishCol)) Is Nothing Then
If Target.Value = 1 Then
If StartTimes.Exists(Target.Row) Then
' Calculate elapsed time
Dim StartTime As Date
StartTime = StartTimes(Target.Row)
ws.Cells(Target.Row, TimeCol).Value = Format(Now - StartTime, "hh:mm:ss")
StartTimes.Remove Target.Row
End If
End If
End If
End Sub