Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Does the validation range still have validation?
If Not HasValidation(Range("A1:A1048576")) Then RestoreValidation
If Not HasValidation(Range("C1:C1048576")) Then RestoreValidation
If Not HasValidation(Range("I1:I1048576")) Then RestoreValidation
If Not HasValidation(Range("P1:P1048576")) Then RestoreValidation
End Sub
Private Sub RestoreValidation()
Application.EnableEvents = False
'turn off events so this routine is not continuously fired
Application.Undo
Application.EnableEvents = True
'and turn them on again so we can catch the change next time
MsgBox "Your last operation was canceled." & _
"It would have deleted data validation rules.", vbCritical
End Sub
Private Function HasValidation(r) As Boolean
' Returns True if every cell in Range r uses Data Validation
On Error Resume Next
Debug.Print r.Validation.Type 'don't care about result, just possible error
If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
我在上面的代碼中應用了4列的驗證,即使驗證通過了我得到4錯誤彈出消息如何限制錯誤消息的數量?避免excel中出現多個錯誤彈出消息
UPDATE:
我選擇從下拉這是一個有效的選擇的價值,但我得到了以下錯誤消息。 我使用下面的代碼
被應用到所有的片材或片材的一些或只是一個片材的驗證? –
@SiddharthRout驗證適用於所有工作表。此代碼寫在ThisWorkBook選項卡下。 – MaheshVarma
因此,如果您看到1張表的消息,那麼您不需要第二張表中的相同消息框? –