0

我有两个具有依赖列表验证的单元格 - 我正在使用 INDIRECT 根据第一个单元格的值获取第二个单元格的列表/选择框。

我需要做的更复杂,我正在努力寻找解决方案。

在 cellA 中,如果选择了 value1,则 cellB 应该有 list1(我现在可以这样做)但是如果在 cellA 中选择了 value2 或 value3,则 cellB 应该需要任何数字 - 而不是列表值。

基本上,在一个实例中,我需要在 cellB 中进行列表验证,但在另一个实例中,我需要一个数字验证 - 任何数字。

有人能帮忙吗 ?欣赏它。

4

1 回答 1

0

我认为他们自动执行此操作的唯一方法是使用 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
于 2013-03-15T00:31:21.560 回答