Excel,想要基于 "yes" 单元格创建下拉列表

Excel, Want to Create Dropdown Based on a "yes" Cell

问题很简单,但我似乎无法解决或找不到答案。

我有一个单元格 A2,它根据用户在别处输入的内容 yes/no 激活。

我想在另一个单元格 A3 中创建一个下拉列表,前提是 A2 中有一个 "yes"。据我所知,这只能通过数据验证来完成,但它会不断抛出错误。

使用下拉列表的数据验证,当您创建它时,您可以使用 "If" 语句。从 "allow" 中选择 "List",然后为来源选择 =if(A2="Yes",B2:B6,C2)。请注意,我假设您的数据列表是 B2:B6,如果 A2 不是 "Yes".

,则在 C2 中是您想要 return 的某个字符串

注意:如果 A2 不是 "Yes",并且您希望 A3 中的列表然后说 "No list necessary",您不能只在 IF 语句中使用该字符串进行验证 - 您需要将该字符串放入,即 C2,并使用上面的公式。出于某种原因,无法将字符串放入数据验证 IF 语句中。

编辑:哦好吧看来 VBA 对于这种用途完全没有必要。我不会删除答案,因为它会起作用,但我不建议使用更简单的方法!

我会为此使用 VBA。如果您不怕弄脏某些代码,那么我相信这里的东西会起作用。如果你是绝对零VBA我可以提供更详细的帮助

打开 Visual Basic 编辑器并copy/paste这个:

'this means it activates when there is change in your worksheet:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)         
    If Not Intersect(Target, Range("A2")) Is Nothing Then 'Checks that the change occurs in the cell A2       

        If Range("A2") = "Yes" Then 'only enters this if it changed to yes
            With Range("A3").Validation 'this little block creates the list. More on this after the code
                .delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=Paramètres!$C:$C"
            End With 
        End If

        If Range("A2") = "No" Then 'only does this if it changed to no
            Range("A3").Validation.delete 'this deletes the list.                    
        End If

    End if
End sub

因此,在我们的例子中,列表是在单元格 A2 中发生更改时创建或删除的。该列表包含您必须在单元格 C1 到 C5 中写入的项目。 (例如:在 c1 中写 "hello",在 c2 中写:"goodbye" 等)。如果您想要更多或更少的输入,您可以根据需要将 C1 更改为 C3 或 C10。您也可以通过更改 C 将列表放在其他地方(例如 E 列)。在列中写入列表后,我建议隐藏该列,因为不需要看到它。