Boton en excel con password
Tengo un botón en excel el cual reinicia un cronometro, deseo que al darle click primero me pida password como le hago, que código le pongo, el botón se llama reiniciar.
Prueba lo siguiente:
Sub reiniciar()
Dim pwd As Variant
pwd = InputBox("Captura el password", "INICIAR")
If pwd = "" Then Exit Sub
If pwd <> "abc" Then
MsgBox "Password incorrecto"
Exit Sub
End If
'
'continuarcon tu código
'
End Sub
Como quedaría con toda mi codificación si es la sig
Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime
StopIt = False
ResetIt = False
If Range("K81") = 0 Then
StartTime = Timer
PauseTime = 0
LastTime = 0
Else
StartTime = 0
PauseTime = Timer
End If
StartIt:
DoEvents
If StopIt = True Then
LastTime = TotalTime
Exit Sub
Else
'
'Desproteger la hoja
ActiveSheet.Unprotect "abc"
'
FinishTime = Timer
TotalTime = FinishTime - StartTime + LastTime - PauseTime
TTime = TotalTime * 100
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("K81").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
If ResetIt = True Then
Range("K81") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
PauseTime = 0
End
End If
'
'Proteger la hoja
ActiveSheet.Protect "abc"
'
GoTo StartIt
End If
End Sub
'
Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
StopIt = True
End Sub
'
Private Sub CommandButton3_Click()
'
'Desproteger la hoja
ActiveSheet.Unprotect "abc"
'
Range("K81").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
ResetIt = True
'
'Proteger la hoja
ActiveSheet. Protect "abc"
'
End Sub
Prueba así:
Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime
Dim pwd As Variant
pwd = InputBox("Captura el password", "INICIAR")
If pwd = "" Then Exit Sub
If pwd <> "abc" Then
MsgBox "Password incorrecto"
Exit Sub
End If
StopIt = False
ResetIt = False
If Range("K81") = 0 Then
StartTime = Timer
PauseTime = 0
LastTime = 0
Else
StartTime = 0
PauseTime = Timer
End If
StartIt:
DoEvents
If StopIt = True Then
LastTime = TotalTime
Exit Sub
Else
'
'Desproteger la hoja
ActiveSheet.Unprotect pwd
'
FinishTime = Timer
TotalTime = FinishTime - StartTime + LastTime - PauseTime
TTime = TotalTime * 100
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("K81").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
If ResetIt = True Then
Range("K81") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
PauseTime = 0
End
End If
'
'Proteger la hoja
ActiveSheet.Protect pwd
'
GoTo StartIt
End If
End Sub
'
Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
StopIt = True
End Sub
'
Private Sub CommandButton3_Click()
'
'Desproteger la hoja
Dim pwd As Variant
pwd = InputBox("Captura el password", "INICIAR")
If pwd = "" Then Exit Sub
If pwd <> "abc" Then
MsgBox "Password incorrecto"
Exit Sub
End If
ActiveSheet.Unprotect pwd
'
Range("K81").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
ResetIt = True
'
'Proteger la hoja
ActiveSheet. Protect pwd
'
End SubAl final de mi respuesta hay un botón para valorar, Excelente, si te funciona la solución.
Si tienes dudas, puedes regresar a comentar.
Funcionaba y ahora m sale un recuadro que dice error 1004 y me pone contraseña incorrecta compruebe que la tecla bloq mayus esta desactivada y que escribió bien la contraseñas y solo quiero que la contraseña sea para el botón 3
Pero el botón1 también escribe en la hoja, entonces debes desproteger la hoja.
No sé cómo tienes o quieres el botón1, pero prueba lo siguiente:
Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime
Dim pwd As Variant
pwd = "abc"
StopIt = False
ResetIt = False
If Range("K81") = 0 Then
StartTime = Timer
PauseTime = 0
LastTime = 0
Else
StartTime = 0
PauseTime = Timer
End If
StartIt:
DoEvents
If StopIt = True Then
LastTime = TotalTime
Exit Sub
Else
'
'Desproteger la hoja
ActiveSheet.Unprotect pwd
'
FinishTime = Timer
TotalTime = FinishTime - StartTime + LastTime - PauseTime
TTime = TotalTime * 100
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("K81").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
If ResetIt = True Then
Range("K81") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
LastTime = 0
PauseTime = 0
End
End If
'
'Proteger la hoja
ActiveSheet.Protect pwd
'
GoTo StartIt
End If
End Sub
- Compartir respuesta