Cómo podemos crear un temporizador de actividad

0

 


Código:


Dim tiempoInicio As Date

Dim timerActivo As Boolean


Sub IniciarTimer()

    tiempoInicio = Now

    timerActivo = True

    MsgBox "Temporizador iniciado"

End Sub


Sub DetenerTimer()    

    If timerActivo = False Then

        MsgBox "Debe iniciar el temporizador antes de finalizar."

        Exit Sub

    End If

    

    Dim duracion As Double

    duracion = Now - tiempoInicio

    

    Dim resultado As String

    Dim totalSegundos As Long

    Dim horas As Long

    Dim minutos As Long

    Dim segundos As Long

    

    totalSegundos = duracion * 86400

    

    horas = totalSegundos \ 3600

    minutos = (totalSegundos Mod 3600) \ 60

    segundos = totalSegundos Mod 60

    

    resultado = Format(horas, "00") & ":" & Format(minutos, "00") & ":" & Format(segundos, "00")

    

    Dim ws As Worksheet

    

    On Error Resume Next

    Set ws = Sheets("Registro")

    On Error GoTo 0

    

    If ws Is Nothing Then

        Set ws = Sheets.Add(After:=Sheets(Sheets.Count))

        ws.Name = "Registro"

        ws.Range("A1:D1").Value = Array("Tarea", "Inicio", "Fin", "Duración")

        ws.Range("A1:D1").Interior.Color = RGB(255, 255, 153)

    End If

    

    Dim fila As Long

    fila = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1

    

    ws.Cells(fila, 1).Value = "Tarea X"

    ws.Cells(fila, 1).Interior.Color = RGB(217, 217, 217)

    

    ws.Cells(fila, 2).Value = tiempoInicio

    ws.Cells(fila, 3).Value = Now

    ws.Cells(fila, 4).Value = resultado

    

    ws.Columns("B:C").NumberFormat = "dd/mm/yyyy hh:mm:ss"

    ws.Columns("B:C").AutoFit

    

    If ws.Columns("B").ColumnWidth < 20 Then ws.Columns("B").ColumnWidth = 20

    If ws.Columns("C").ColumnWidth < 20 Then ws.Columns("C").ColumnWidth = 20

    

    ws.Activate

    ws.Cells(fila, 1).Select

    

    MsgBox "Duración: " & resultado

    

    timerActivo = False

End Sub


⭐ Si te gustó, por favor regístrate en nuestra Lista de correo y Suscríbete a mi canal de YouTube para que estés siempre enterado de lo nuevo que publicamos

Entradas que pueden interesarte

Sin comentarios

Etiquetas:
#aprendiendoexcel365, #josealcaldealias, #Excel, #funciones, #UDF, #Visual Basic, #VBA, #DAX, #Tips, #Basico, #Microsoft Excel, #Cursos Excel OnLine, #Aprende Excel, #Aprende Excel OnLine, #Excel 365, #Power Pivot, #Power BI, #Power Query, #Google Sheets, #Macros, #Código VBA, #Tutoriales Excel, #MTV, #Vbscript, #TypeScript, #Lenguaje M, #fórmulas, #funciones, #paso a paso, #funciones Excel, #libros, #tablas, #formatos, #hojas, #datos, #gráfico, #análisis de datos, #base de datos, #dashboards, #tablas dinámicas, #excel desde cero, #hoja de cálculo, #plantillas de Excel