简体   繁体   English

在“数据验证”下拉列表中设置默认值

[英]Setting default values in Data Validation drop-down list

I have many data validation (DV) cells with drop-down list which I have set up by menu Data > Data Validation > list . 我有许多数据验证(DV)单元格,下拉列表是我通过菜单Data > Data Validation > list Is there a bulk way in VBA to set them all to default values? VBA中有一种批量方式可以将它们全部设置为默认值吗? By default value I mean either: 默认值我的意思是:

  1. the first value declared in a DV list DV列表中声明的第一个值
  2. or value which is not on the DV list, something like Choose item from the list . 或者不在DV列表中的值,例如列表中的Choose item from the list

The second option might be useful if we want the user to be responsible for his choice. 如果我们希望用户对他的选择负责,那么第二个选项可能会有用。 As soon as the user clicks on a data validation cell, he is forced to choose something. 一旦用户点击数据验证单元格,他就被迫选择一些东西。 There is no option to leave default value Choose item from the list because such value is not on the validation list. 没有保留默认值的选项Choose item from the list因为此类值不在验证列表中。 So the user cannot say later "I didn't vote". 所以用户以后不能说“我没投票”。

To use an offset from columns C to Z in the current row: 要使用当前行中CZ列的偏移量:

  • select any cell in the first row 选择第一行中的任何单元格
  • create a named range ( Formulas > Name Manager > New... ) with Name: eg validation and Refers To: would be your formula: 使用名称创建命名范围( Formulas > Name Manager > New...Name:例如validation和参考Refers To:将是您的公式:

     =OFFSET($C1;0;0;1;COUNTA($C1:$Z1)) 
    • english-locale users, use , instead of ; 英语 - 语言环境用户,使用,而不是; as the list separator 作为列表分隔符
  • select cells and apply Data Validation > Allow: List , Source: =validation 选择单元格并应用Data Validation >允许: List ,来源: =validation

When you select a cell in 2nd row and observe the Name Manager , you will notice that the formula is using relative references to the current row. 当您在第二行中选择一个单元格并观察Name Manager ,您会注意到该公式正在使用对当前行的相对引用。


To populate cells with the default value, you can use following formula (as a normal formula inside a cell, it does not have anything to do with the data validation feature whatsoever): 要使用默认值填充单元格,可以使用以下公式(作为单元格内的常规公式,它与数据验证功能无关):

=INDEX(validation, 1)

and when you actually select a value from the drop-down list, the formula will be overwritten by the selected value, so when you change the 1st item in your list, the value will not change for explicitly selected cells. 当您从下拉列表中实际选择一个值时,该公式将被所选值覆盖,因此当您更改列表中的第一个项目时,对于明确选择的单元格,该值不会更改。

This is what I end up with. 这就是我最终的结果。

Sub DropDownListToDefault()
    Dim oCell As Range

    For Each oCell In ActiveSheet.UsedRange.Cells
        If HasValidation(oCell) Then
            oCell.Value = "'- Choose from the list -"
        End If
    Next
End Sub

Function HasValidation(cell As Range) As Boolean
    Dim t: t = Null

    On Error Resume Next
    t = cell.Validation.Type
    On Error GoTo 0

    HasValidation = Not IsNull(t)
End Function

The function HasValidation is stolen from here . HasValidation函数从此处被盗。

After running the macro DropDownListToDefault you will have the following choice after clicking in a DV cell: 运行宏DropDownListToDefault后,在单击DV单元格后,您将有以下选择:

在此输入图像描述

Note that in the drop-down list there is no item like - Select from the list - If you want a user to choose something from the drop-down list simply do not accept the default value in further processing. 请注意,在下拉列表中没有类似项目- Select from the list -如果您希望用户从下拉列表中选择某些内容,则只需在进一步处理中不接受默认值。

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

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