简体   繁体   English

用过滤后的数据填充下拉列表

[英]Populate a dropdown list with filtered data

I am trying to find a way to make a dropdown list that only contains items if a condition is met in a different column of the source table. 我试图找到一种方法来制作仅在源表的不同列中满足条件的情况下才包含项的下拉列表。

So basically I have a table with a list of all jobs, past and present. 所以基本上我有一张桌子,上面列出了过去和现在的所有工作。 Some have been completed and some are still open. 有些已经完成,有些仍然开放。 Column A contains the job numbers which are all unique and column P lets the user know if the job is open or closed. 列A包含唯一的作业编号,列P让用户知道作业是打开还是关闭。 If the job is closed the cell contains a "Y", if the job is still open the cell is empty. 如果作业已关闭,则该单元格包含“ Y”;如果作业仍处于打开状态,则该单元格为空。

I have another table which has a dropdown list which allows the user to select the job they are working on. 我还有另一个表,该表具有一个下拉列表,该列表允许用户选择他们正在处理的作业。 To keep the size of the dropdown list reasonable I only want it to contain the job numbers of jobs that are still open. 为了使下拉列表的大小合理,我只希望它包含仍处于打开状态的作业的作业编号。 Is there a way to use an IF statement or similar to only populate the dropdown list with job numbers from column A if column P is empty? 如果P列为空,是否可以使用IF语句或类似方法仅使用A列的作业编号填充下拉列表?

I've found several examples of using one dropdown list to change the items in a different dropdown list but it seems like they all require the source data to be known and in named ranges beforehand, whereas for me this is going to be a dynamic list as jobs are closed and others are added. 我发现了几个使用一个下拉列表更改另一个下拉列表中项目的示例,但似乎它们都需要事先知道源数据并在命名范围内,而对我来说这将是一个动态列表关闭工作并添加其他工作。 So far I've been unable to find anything I can apply to my situation. 到目前为止,我一直找不到能适用于我的情况的任何东西。

I think the easiest way to make the list, is to use a macro to do it. 我认为制作列表的最简单方法是使用宏来完成。

If you go through the column, and then put every job that isn't closed in an Array, 如果您浏览该列,然后将所有未关闭的作业放入Array中,
you can then put the array as the list. 然后可以将数组作为列表。

Sub makeList()
Dim arr() As Variant, i As Integer, lnght As Long
lnght = Range("A" & Rows.Count).End(xlUp).Row
i = 0
For Each cell In Range("A1:A" & lnght)
    If Not cell.Offset(0, 15).Value = "Y" Then
        ReDim Preserve arr(0 To i) As Variant
        arr(i) = cell.Value
        i = i + 1
    End If
Next

With Range("C1").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:=Join(arr, ",")
End With
End Sub

This puts the drop down list in "C1", so just change that to where it's supposed to be. 这会将下拉列表放在“ C1”中,因此只需将其更改为应有的位置即可。

This needs to be updated every time a job is added, removed, or changed to closed. 每次添加,删除或更改作业为关闭作业时,都需要对此进行更新。 You could have an update button, or just put the code in the worksheet. 您可以有一个更新按钮,或者只是将代码放在工作表中。 I'd suggest calling it with a Worksheet_Change , and if you don't want it to run on every change, add an intersect method to validate if the target of the change is relevant to the list. 我建议使用Worksheet_Change调用它,如果您不希望它在每次更改上都运行,请添加一个相交方法以验证更改的目标是否与列表相关。

Also this assumes the delimiter to be ",", so change that accordingly if needed. 同样,这也假定定界符为“,”,因此,如果需要,可以相应地进行更改。

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

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