设置您的验证值(名称列表)。与掉期(缩短名称)搭配
唯一的收获是您的VBA会触发自身:该函数会更改单元格值,该函数会触发单元格更改事件,从而触发相同的功能等。在下面,我通过设置标志破坏了该循环,但可能还有其他解决方案。
Public DisabledFlag As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim NewValue As Variant
If DisabledFlag = True Then
DisabledFlag = False
GoTo Disabled
End If
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("D4:D12")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Use the current value to get the 'real' value ...
On Error Resume Next
NewValue = WorksheetFunction.XLookup( _
Target.Value, _
Range("A4:A6"), _
Range("B4:B6"), _
"Not found")
On Error GoTo 0
' Setting the value will trigger this function ...
' Ignore that event.
DisabledFlag = True
Target.Value = NewValue
End If
Disabled:
End Sub