【发布时间】:2015-06-19 18:18:25
【问题描述】:
因此,我在一个单元格中进行了数据验证,该单元格根据另一个单元格中的用户选择更改为三个不同命名范围之一。我需要的是当用户选择某个值时,即“选择 A”,数据验证不仅会更改为相应的命名范围,还会显示该范围内的第一个值。
目前我可以操纵代码来获取默认值,但是每次我尝试进行选择时它都会变回默认值。这甚至可能吗?下面是我当前使用 Worksheet_Change 事件针对特定命名范围运行的代码
在此示例中,我使用了命名范围 selection_a
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$E$3").Value = "Selection A" Then
With Range("L3:R4").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=selection_a"
.IgnoreBlank = False
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
默认值不断显示并变回:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$E$3").Value = "Selection A" Then
Range("$L$3").Value = Sheets("sheet2").Range("$M$4").Value
With Range("L3:R4").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=selection_a"
.IgnoreBlank = False
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
【问题讨论】:
-
您可能应该禁用事件处理代码中的事件,因为您正在更改单元格。相关:stackoverflow.com/questions/13860894/…
-
@Byron 很好,将其添加到我的答案中