【问题标题】:Create a validation in a cell based on the value in another cell根据另一个单元格中的值在一个单元格中创建验证
【发布时间】:2021-12-16 07:34:48
【问题描述】:

我有 2 个单元格 C7 和 C8,我需要创建一个数据验证,如果 C7 为 0,那么在 C8 中应该只允许 0-12,如果在 C8 中输入的值大于 12,它会显示错误消息,如果 C7 大于 0,则 C8 中将允许任何数字。

【问题讨论】:

  • C7 本身是否经过验证以仅允许数字输入?如果不是,C8 中的验证应该如何处理C7 中的非数字?

标签: excel vba validation excel-formula


【解决方案1】:

将此代码插入到工作表模块

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C7")) Is Nothing Then
        If Range("C7").Value = 0 Then
            Call DataValidation_Create
        Else
            Call DataValidation_Delete
        End If
    End If
End Sub

将其插入标准模块

Sub DataValidation_Create()
    With Range("C8").Validation
        .Delete
        .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="0", Formula2:="12"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = "Choose a value"
        .ErrorTitle = "Choose a value between 0 - 12"
        .InputMessage = "Insert a value between 0  - 12"
        .ErrorMessage = "Insert a value between 0  - 12"
        .ShowInput = True
        .ShowError = True
    End With
End Sub

Sub DataValidation_Delete()
    With Range("C8").Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
End Sub

【讨论】:

    【解决方案2】:

    我喜欢 Elio Fernandes 的解决方案,唯一的一点是,在您更改 C8 之后,您可以在之后更改 C7 并购买通过验证检查。 (我尝试先建议对他的帖子进行编辑,但我害怕搞砸一些事情) 这只是他的解决方案的副本,经过修改以包括 C7 上的验证。

    注意_SelectionChange 中设置单元格选择验证的区别。

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Not Intersect(Target, Range("C7:C8")) Is Nothing Then
            If Range("C7").Value = 0 Then
                Call DataValidation_Create(Range("C8"))
            Else
                Call DataValidation_Delete(Range("C8"))
            End If
    
            If Range("C8").Value > 12 Then
                Call DataValidation_Create(Range("C7"))
            Else
                Call DataValidation_Delete(Range("C7"))
            End If
        End If
    
    End Sub
    
    
    Sub DataValidation_Create(pCell As Range)
        If pCell.Address = "$C$8" Then
            With Range("C8").Validation
                .Delete
                .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, 
                     _Operator:=xlBetween, Formula1:="0", Formula2:="12"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = "Choose a value"
                .ErrorTitle = "Choose a value between 0 - 12"
                .InputMessage = "Insert a value between 0  - 12"
                .ErrorMessage = "Insert a value between 0  - 12"
                .ShowInput = True
                .ShowError = True
            End With
        ElseIf pCell.Address = "$C$7" Then
            With Range("C7").Validation
                .Delete
                .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, 
                     _Operator:=xlGreater, Formula1:="0"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = "Choose a value"
                .ErrorTitle = "Choose a value greater than 0"
                .InputMessage = "Choose a value greater than 0"
                .ErrorMessage = "Choose a value greater than 0"
                .ShowInput = True
                .ShowError = True
            End With
        End If
    End Sub
    
    Sub DataValidation_Delete(pCell As Range)
        With pCell.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, _
                  Operator:=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = True
            .ShowError = True
        End With
    End Sub 
    

    【讨论】: