I'm using the following code to detect a change in a specific column, if the user enters an invalid character I want to change the cells background color.
I thought ActiveCell
was what I should use, but that returns a runtime error.
This is the code I have so far.
Private Sub Worksheet_Change(ByVal Target As Range)
activeRow = ActiveCell.Row
ActiveColumn = ActiveCell.Column
If ActiveColumn = 9 Then
Other code here...
Else
If ActiveColumn = 7 Then
If Target Like "*[!%^:~#|@.;`\/""*$,]*" Then
ActiveCell.Interior.ColorIndex = 3
End If
End If
End If
End Sub
Could someone advise how to set the background of the cell that has just been edited.
Thanks
UPDATE. I've stripped this back to the basics and I can get it to work using:
Private Sub Worksheet_Change(ByVal Target As Range)
'If Target Like "*[\!%^:~#|@.;`\/""*$,]*" Then
Target.Interior.ColorIndex = 3
'End If
End Sub
Any cell I change gets a red background, But as soon as I uncomment the IF/End If
it stops working. I don't understand why.
CodePudding user response:
My issue was caused by the cell I was trying to update being locked and the worksheet protected. As soon as I removed that it the update worked correctly.
I've added some code to the sheet to unprotect the worksheet, update the cell and then re protect the sheet.