Private Sub Worksheet_Change(ByVal Target As Range)
irow = Target.Row
If IsNumeric(Range("B" & irow)) = False Then
Range("B" & irow).ClearContents
MsgBox "警告:B" & irow & "只能输入数字。"
Exit Sub
ElseIf Range("B" & irow) > Range("A" & irow) Then
Range("B" & irow).ClearContents
MsgBox "警告:B" & irow & "不能大于" & Range("A" & irow) * 1
Exit Sub
End If
End Sub
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then '如果B列
'触发多个单元格时,作清空当前选区处理
If Target.Count > 1 Then
Application.EnableEvents = False
Target.Clear
MsgBox "不允许多个输入!"
Application.EnableEvents = True
Exit Sub
End If
'如果A列非空且值小于B列
If Target.Offset(, -1) <> "" And Target > Target.Offset(, -1) Then
Application.EnableEvents = False '临时关闭事件,防止自身循环触发
Target = "" '满足条件,清空目标
Application.EnableEvents = True
MsgBox "禁止输入!"
End If
End If
End Sub
复制以上代码粘贴到你要监控的Sheet里。
注:不支持多单元格一起输入,或拖拽。