我认为他们自动执行此操作的唯一方法是使用 VBA 应用正确类型的验证。
以下示例(您不能按原样插入)应该为您提供一些关于您需要做什么的线索。此代码将放置在您正在使用的工作表的工作表代码模块中。
您可以使用宏记录器获取代码以生成所需的验证。
如果您可以使用您的代码和公式更新您的问题,我也许可以根据您的需要对其进行更多修改。
Private Sub Worksheet_Change(ByVal Target As Range)
' Target is a reference to the Cell(s) that have changed.
If Target.Address = "$A$1" Then
' We only care about this cell (CellA)
Select Case Target.Value
Case "needalist"
' remove any existing validation and add the list validation to $B$1 (CellB)
With Sheet1.Range("$B$1").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=$C$1:$C$7" ' Formula1 Can contain a formula
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Case "needanumber"
' remove any existing validation and add the number validation to $B$1 (CellB)
With Sheet1.Range("$B$1").Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="1", Formula2:="10" ' Formula1 is min, Formula2 is max
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Case Else
' Catch Everything
Sheet1.Range("$B$1").Validation.Delete
End Select
End If
End Sub