我正在尝试验证M31:M41中的某个单元格范围,如果这些单元格是灰色的并且用户错误输入了信息,则会出现一个消息框,然后撤消上一个操作。到目前为止,这是我的代码
v
问题在于,它可以按我的要求运行,但会给我错误
运行时错误'1004':对象'_Application'的方法'Undo'失败。
有没有实现任何编码问题的想法?
答案 0 :(得分:1)
这似乎适合我一个单元格。...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRange As Range
Set myRange = Range("M31:M41")
If Not Intersect(Target, myRange) Is Nothing Then
If Target.Interior.Color = 15 Then
Application.EnableEvents = False
MsgBox "Cannot Change This Cell"
Application.Undo
Application.EnableEvents = True
End If
End If
End Sub
要进行多范围更改...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRange As Range, xCell As Range
Set myRange = Range("M31:M41")
For Each xCell In Target
If Not Intersect(xCell, myRange) Is Nothing Then
If xCell.Interior.Color = 15 Then
Application.EnableEvents = False
MsgBox "Cannot Change This Cell"
Application.Undo
Application.EnableEvents = True
Exit Sub
End If
End If
Next xCell
End Sub