简体   繁体   English

VBA - 启用和禁用验证

[英]VBA - enable and disable validation

I would like to ask is there a way to enable and disable data validation on excel? 我想问一下有没有办法在excel上启用和禁用数据验证? I would like to disable validation on a certain input,'others'. 我想禁用某些输入的验证,'其他'。 And enable it if the input is not others. 如果输入不是其他输入,则启用它。

I had a list data validation, when option cell== others , user is able to free text. 我有一个列表数据验证,当选项cell== others ,用户可以释放文本。 Else disable free text. 否则禁用自由文本。

My list validation is done by the excel build in data validation, not using VBA. 我的列表验证是通过数据验证中的excel构建完成的,而不是使用VBA。

This is the code that I have tried: 这是我尝试过的代码:

Sub Remove_Data_Validation_Selection()

Selection.Validation.Delete

End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Cells(2, 6).Value = "Others" Then
            Selection.Validation.Delete
    Else
           //Enable the validation
    End If
End Sub

Right now, when 'Others' is being selected, the validation will be DELETED. 现在,当选择“其他”时,验证将被删除。 How do I enable it back? 我该如何启用它? As Selection.Validation.Delete deletes the object, will I be able to enable it back? Selection.Validation.Delete删除对象时,我能够将其启用吗?

If you want to use it in the same procedure, declare object variable 如果要在同一过程中使用它,请声明对象变量

Dim Valid As Validation

and assign value before deleting. 并在删除前指定值。 EDIT here. 在这里编辑。

If Cells(2, 6).Value = "Others" Then
        set Valid = Selection.Validation
        Selection.Validation.Delete

If you want to set up validation in other procedure: 如果要在其他过程中设置验证:

SomeRange.Validation = valid

I don't know why you would do it, but this is answer for your question. 我不知道你为什么会这样做,但这是你的问题的答案。 However, if you want to preserve validation for another procedure, you could do this way: 但是,如果要保留另一个过程的验证,可以这样做:

Dim wsHid As Worksheet
Dim rngValid As Range

Set rngValid = Selection
Set wsHid = ThisWorkbook.Worksheets.Add
wsHid.Visible = xlSheetVeryHidden
rngValid.Copy
wsHid.Range("A1").PasteSpecial Paste:=xlPasteValidation

and then, if you need recreate validation, you just copy it from wsHid.Range("A1") and PasteSpecial Paste: xlPasteValidation. 然后,如果需要重新创建验证,只需从wsHid.Range(“A1”)和PasteSpecial Paste:xlPasteValidation复制它。 Of course, this is just bunch of ideas, you should adjust it to your code. 当然,这只是一堆想法,你应该根据你的代码进行调整。

This is exactly what I'm looking for, when user chooses other, it will modify the validation setting. 这正是我正在寻找的,当用户选择其他时,它将修改验证设置。 You can change the modify to delete if you would like to remove the validation. 如果要删除验证,可以将修改更改为删除。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address = "$F$2" Then
    If Target.Value = "Others" Then
        With Target.Validation
        .Delete
        .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween
        .IgnoreBlank = False
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    End
    End If
End If

If Target.Address = "$F$2" Then
  With Target.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="1,2,3,Others"
        .IgnoreBlank = False
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
  End With
End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM