Hello Forum,
I have a workbook with 2 VBA codes. The workbook and the codes work fine until I protect the sheet with password. It stills give me the correct result but as the same time it gives the error:
Run-time error '1004:
Application-defined or object-defined error
How can I remove this error message when the work sheet is protected with password?
Thank you so much for your help
Thup_98
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sPrompt As String, iReply As Integer, i As Integer
If Intersect(Target, Me.Range("A2:A9")) Is Nothing Or Target.Count > 1 Then
Exit Sub
End If
If Target.Row = 9 Then
sPrompt = "Are you sure you want to reset all data validations?"
iReply = MsgBox(sPrompt, vbYesNo)
If iReply = 6 Then Me.Range("C2:C8,F2:F8,I2:I8,N2:N8") = "NA"
If iReply = 6 Then Me.Range("D2:D8,G2:G8,J2:J8,O2:O8") = 0
Else
i = Target.Row
sPrompt = "Are you sure you want to reset all data validations in row " & i & "?"
iReply = MsgBox(sPrompt, vbYesNo)
If iReply = 6 Then Me.Range("C" & i & ",F" & i & ",I" & i & ",N" & i) = "NA"
If iReply = 6 Then Me.Range("D" & i & ",G" & i & ",J" & i & ",O" & i) = 0
End If
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim part As String
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
With Target
If .Column = 3 Or .Column = 6 Or .Column = 9 Or .Column = 14 Then
If .Value = "NA" Then .Offset(0, 1).Value = 0
End If
End With
If Target.Column = 14 And Target.Value = "_9000_0001CT" Then
Target.Offset(0, 1).Value = 1
End If
If Target.Column = 14 And Target.Value = "_9000_0001" Then
Target.Offset(0, 1).Value = 1
End If
If Target.Column = 14 And Target.Value = "_9000_0008_220V" Then
Target.Offset(0, 1).Value = 1
End If
If Target.Column = 14 And Target.Value = "_9000_0001CT" Then
Target.Offset(0, 1).Value = 1
End If
If Target.Column = 14 And Target.Value = "_9000_0002" Then
Target.Offset(0, 1).Value = 1
End If
If Target.Column = 4 And Target.Offset(0, -1).Value <> "NA" Then
part = Target.Offset(0, -1).Value
Target.Offset(0, -1).Value = Right(Replace(part, "_", "-"), Len(part) - 1)
End If
If Target.Column = 7 And Target.Offset(0, -1).Value <> "NA" Then
part = Target.Offset(0, -1).Value
Target.Offset(0, -1).Value = Right(Replace(part, "_", "-"), Len(part) - 1)
End If
If Target.Column = 10 And Target.Offset(0, -1).Value <> "NA" Then
part = Target.Offset(0, -1).Value
Target.Offset(0, -1).Value = Right(Replace(part, "_", "-"), Len(part) - 1)
End If
If Target.Column = 15 And Target.Offset(0, -1).Value <> "NA" Then
part = Target.Offset(0, -1).Value
Target.Offset(0, -1).Value = Right(Replace(part, "_", "-"), Len(part) - 1)
End If
End Sub
Bookmarks