我的工作表设置了数据验证下拉菜单,我想要一个宏只在单元格的值从下拉列表中的另一个值更改时触发,而不是从默认的“空”值。
以下是我要使用的内容:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 Then
If IsEmpty(Target.Value) = True Then
MsgBox "Test1"
Else
MsgBox "Test2"
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
我的问题是这个“IsEmpty”命令正在读取选择之后的单元格。我希望它能够在选择之前读取单元格的值。
我该怎么做?
答案 0 :(得分:2)
示例方法:
Const COL_CHECK As Long = 5
Private oldVal
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Target.Cells(1) '<< in case multiple cells are changed...
If c.Column = COL_CHECK Then
If oldVal <> "" Then
Debug.Print "changed from non-blank"
Else
Debug.Print "changed from blank"
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
Set c = Target.Cells(1)
oldVal = IIf(c.Column = COL_CHECK, c.Value, "")
Debug.Print "oldVal=" & oldVal
End Sub
答案 1 :(得分:1)
另一种方法: 每个验证下拉列表需要一个单元格:
{{1}}
在另一个单元格中,假设下拉列表位于E6中: = E6&安培; ValChange(E6)
application.caller.text将是旧值 (计算必须是自动的)