Worksheet_Change Event

Joined
Jun 9, 2005
Messages
1
Reaction score
0
Hi,

In an attempt to protect my data validation from being deleted, I have written the following code. However, it seems to run into a 1004 error and a circular reference that Excel cannot list. Does anybody know where I am going wrong? More importantly, is this the right approach?


code:
--------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)

Dim DataRange As Range
Dim c As Range
Dim mycount As Integer

Worksheets("Sheet1").Unprotect "123456"

If Range("F12:G21").SpecialCells(xlCellTypeAllValidation).Count = 20 Then
GoTo 1:
Else
Application.Undo
Application.EnableEvents = False
MsgBox "Cannot delete data validation rules." & vbNewLine & _
"Please use Paste Special/Values Command.", vbCritical, "ERROR!"
Worksheets("Sheet1").Protect "123456"
Application.EnableEvents = True
Exit Sub
End If
1:
Application.EnableEvents = False
Set DataRange = Range("F12:G21")
mycount = 0

ActiveSheet.CircleInvalid

For Each c In DataRange
If Not c.Validation.Value Then
mycount = mycount + 1
End If
Next

If mycount = 0 Then Range("L10").Value = "0"
If mycount <> 0 Then
Range("L10").Value = "ERROR! THERE ARE INVALID ENTRIES. SEE HIGHLIGHTED CELLS."
End If
Worksheets("Sheet1").Protect "123456"
Application.EnableEvents = True
Exit Sub

End Sub
--------------------------------------------------------------------------------
Any help is appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top