Private Sub Worksheet_Change(ByVal Target As Range)
Dim tasksheet As Worksheet
Dim i As Integer
Dim T2 As Integer
Dim Lr As Integer
ActiveSheet.Unprotect Password:="123"
Target.Locked = False
Set tasksheet = ThisWorkbook.Sheets("Sheet1")
Lr = tasksheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lr
If tasksheet.Cells(i, "A").Value <> "" And tasksheet.Cells(i, "B").Value = "" Then
tasksheet.Cells(i, "B").Value = Time
tasksheet.Cells(i, "B").NumberFormat = "hh:mm"
tasksheet.Cells(i, "A").Font.ColorIndex = 5
End If
If tasksheet.Cells(i, "A") < 18.9 Then
T2 = T2 + 1
tasksheet.Cells(i, "A").Font.ColorIndex = 3
End If
Next
Cells(5, 10).Value = T2
ActiveSheet.Protect Password:="123"
End Sub
Whenever I ran the code, I receive a run time error 1004, on line (tasksheet.Cells(i, "B").NumberFormat = "hh:mm"). If password protection line is removed the code run with no problem
Dim tasksheet As Worksheet
Dim i As Integer
Dim T2 As Integer
Dim Lr As Integer
ActiveSheet.Unprotect Password:="123"
Target.Locked = False
Set tasksheet = ThisWorkbook.Sheets("Sheet1")
Lr = tasksheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lr
If tasksheet.Cells(i, "A").Value <> "" And tasksheet.Cells(i, "B").Value = "" Then
tasksheet.Cells(i, "B").Value = Time
tasksheet.Cells(i, "B").NumberFormat = "hh:mm"
tasksheet.Cells(i, "A").Font.ColorIndex = 5
End If
If tasksheet.Cells(i, "A") < 18.9 Then
T2 = T2 + 1
tasksheet.Cells(i, "A").Font.ColorIndex = 3
End If
Next
Cells(5, 10).Value = T2
ActiveSheet.Protect Password:="123"
End Sub
Whenever I ran the code, I receive a run time error 1004, on line (tasksheet.Cells(i, "B").NumberFormat = "hh:mm"). If password protection line is removed the code run with no problem