根据另一个单元格中的值在一个单元格中创建验证
Create a validation in a cell based on the value in another cell
我有 2 个单元格 C7 和 C8,我需要创建一个数据验证,如果 C7 为 0,则 C8 中只允许使用 0-12,如果在 C8 中输入的值大于12,如果C7大于0,则C8中允许任何数字。
将此代码插入 工作表模块
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
我喜欢 Elio Fernandes 的解决方案,唯一的问题是在更改 C8 之后,您可以随后更改 C7 并购买通过验证检查。
(我试着先建议对他的 post 进行编辑,但我害怕弄乱某些东西)
这只是他的解决方案的副本,修改后也包括 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" 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" 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
我有 2 个单元格 C7 和 C8,我需要创建一个数据验证,如果 C7 为 0,则 C8 中只允许使用 0-12,如果在 C8 中输入的值大于12,如果C7大于0,则C8中允许任何数字。
将此代码插入 工作表模块
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
我喜欢 Elio Fernandes 的解决方案,唯一的问题是在更改 C8 之后,您可以随后更改 C7 并购买通过验证检查。 (我试着先建议对他的 post 进行编辑,但我害怕弄乱某些东西) 这只是他的解决方案的副本,修改后也包括 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" 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" 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