excel下拉列表数据筛选
Someone asked me how to make a data validation drop down that only shows the visible rows from a filtered list. I created a sample file that shows how you can do that, and here are the details on setting up a drop down from filtered Excel list.
有人问我如何使数据验证下拉列表仅显示过滤列表中的可见行。 我创建了一个示例文件来显示如何执行此操作,这是从过滤的Excel列表中设置下拉菜单的详细信息。
视频:从过滤列表中下拉 (Video: Drop Down from Filtered List)
Update: Here’s a new video that shows how to set up the drop down from a filtered list, step by step. The written instructions are below the video.
更新 :这是一个新视频,逐步显示如何从过滤列表中设置下拉菜单。 书面说明在视频下方。
员工名单 (Employee List)
In this example, the workbook has a list of employees, in a formatted Excel table (tblEmp).
在此示例中,工作簿在格式化的Excel表 (tblEmp)中具有一个雇员列表。
There is a named range – EmpList – based on the Employees column in that table.
基于该表中的“雇员”列,有一个命名范围 – EmpList。
简单的下拉列表 (Simple Drop Down List)
If you create a data validation drop down list, it shows all the items from the source list. There are basic instructions for setting up a drop down list on my Contextures website.
如果创建数据验证下拉列表,它将显示源列表中的所有项目。 在我的Contextures网站上有设置下拉列表的基本说明 。
In the sample workbook, there is a schedule sheet, with a drop down list based on the EmpList named range.
在示例工作簿中,有一个计划表,其中有一个基于EmpList命名范围的下拉列表。
Click the arrow in the Staff column, and select any of the employee names.
单击“员工”列中的箭头,然后选择任何员工名称。
过滤名称列表 (Filtered List of Names)
In the Employee table, there is a column to mark an X, for anyone who’s on vacation. To see who’s available for work, you filter the list, and hide all the rows that contain an X in the Vacation column.
在Employee表中,有一个列标记X,用于正在休假的任何人。 要查看谁可以上班,请过滤列表,然后在Vacation列中隐藏所有包含X的行。
That doesn’t affect the data validation drop down list of employees though, on the Schedule sheet.
但是,这不会影响“计划”表上的数据验证下拉雇员列表。
Even though only 6 names are showing, the drop down list still includes all of the employee names.
即使仅显示6个姓名,下拉列表仍包含所有员工姓名。
编号可见行 (Number the Visible Rows)
To create an employee list that only includes the visible rows, we’ll add a new column to the Employee table.
要创建仅包含可见行的员工列表,我们将向员工表添加一个新列。
The new column has the following formula, using the SUBTOTAL function:
使用SUBTOTAL函数 ,新列具有以下公式:
=–SUBTOTAL(3,B$3:B3)
=-小计(3,B $ 3:B3)
- The function has COUNTA (3) as the first argument
该函数的第一个参数为COUNTA(3)
- The range starts at cell B$3 (locked row), and goes down to the current row, B3 (not locked)
范围从单元格B $ 3(锁定行)开始,然后向下到当前行B3(未锁定)
Here is the table with all rows visible, showing the SUBTOTAL formula results in each row. There are cells at the top that link to the first 2 rows, so we can check the results, after some of the rows are hidden.
这是所有行均可见的表,显示每一行的SUBTOTAL公式结果。 顶部有一些单元格链接到前两行,因此在某些行被隐藏之后,我们可以检查结果。
To learn more about numbering visible rows with SUBTOTAL, read this article, that I wrote a few years ago. It explains why the two minus signs are at the start of the formula.
要了解有关使用SUBTOTAL为可见行编号的更多信息,请阅读我几年前写的这篇文章 。 它解释了为什么两个负号位于公式的开头。
将过滤器应用于列表 (Apply a Filter to the List)
Next, the Employee list is filtered, so hide all the rows marked with an X.
接下来,将过滤 Employee 列表 ,因此隐藏标记有X的所有行。
Here is a screen shot of the filtered list, and the visible rows are numbered from 1 to 6.
这是过滤列表的屏幕截图,可见行的编号从1到6。
The SUBTOTAL function ignores rows that have been hidden by a filter, so it will only calculate a count on visible rows
SUBTOTAL函数将忽略已被过滤器隐藏的行,因此它将仅计算可见行的计数
The formulas at the top show that there is a 0 result for the SUBTOTAL function in the hidden row for D3
顶部的公式显示D3的隐藏行中SUBTOTAL函数的结果为0
创建可用员工列表 (Create a List of Available Employees)
Now that the full list is numbered, we’ll create a second list of employees. The new list will have formulas, to get the names, based on the numbers in each row.
现在完整的列表已编号,我们将创建第二个员工列表。 新列表将基于每行中的数字包含公式,以获取名称。
The new list has the headings, ID and Emp, and numbers are typed in the ID column. Be sure to add at least as many numbers as there are employees in the main list – I added a few extra numbers too.
新列表具有标题,ID和Emp,并在ID列中键入数字。 确保添加的号码至少与主列表中的员工人数一样–我也添加了一些额外的号码。
In the Emp column add this formula, to return the employee name, based on the ID numbers.
在Emp列中添加此公式,以根据ID号返回员工名称。
=IFERROR(INDEX(tblEmp[Employees], MATCH(B2,tblEmp[ListNum],0)),””)
= IFERROR(INDEX(tblEmp [Employees],MATCH(B2,tblEmp [ListNum],0)),“”)
The INDEX function returns a name from the Employees column, for the row that has the same number as the ID.
INDEX函数从“雇员”列返回名称,该行的编号与ID相同。
This screen shot shows the new list, with the numbered names from the filtered main list.
此屏幕快照显示了新列表,并带有过滤后的主列表中的编号名称。
计算可见名称 (Count the Visible Names)
We want to use this short list for our drop down list, so we’ll create a dynamic named range with the names in our new list.
我们希望将这个简短列表用于下拉列表,因此我们将使用新列表中的名称创建一个动态的命名范围。
To make the named range the correct size, a formula on the worksheet get the number of visible names.
为了使命名范围正确,工作表上的公式将获得可见名称的数量。
To get the count, the cell contains this MAX formula:
要获取计数,该单元格包含以下MAX公式 :
=MAX(tblEmp[ListNum])
= MAX(tblEmp [ListNum])
That returns the highest number from the main list.
从主列表中返回最高的数字。
创建动态命名范围 (Create a Dynamic Named Range)
To create a dynamic named range, that will adjust its number of rows based on the count of visible names, follow these steps:
要创建一个动态命名范围 ,它将根据可见名称的数量调整行数,请按照以下步骤操作:
- On the Ribbon’s Formulas tab, click Define Name
在功能区的“公式”选项卡上,单击“定义名称”
Type the name: EmpListAvail
键入名称: EmpListAvail
- For the Scope, select Workbook
对于范围,选择工作簿
- In the Refers to box, enter this formula:
在“引用”框中,输入以下公式:
=ListDV!$C$2:INDEX(tblEmpAvail[Emp], ListDV!$F$1)
= ListDV!$ C $ 2:INDEX(tblEmpAvail [Emp],ListDV!$ F $ 1)
- Click OK
点击确定
检查动态范围 (Check the Dynamic Range)
To check the Dynamic Range, follow these steps:
要检查动态范围,请按照下列步骤操作:
- On the Ribbon’s Formulas tab, click Define Name
在功能区的“公式”选项卡上,单击“定义名称”
Click on the new name: EmpListAvail
单击新名称: EmpListAvail
- Click in the Refers to box, and the range will be highlighted on the worksheet, with “marching ants”.
单击“引用”框,范围将在工作表上突出显示,并带有“行进蚂蚁”。
- Click Close.
单击关闭。
公式如何运作 (How the Formula Works)
The formula for the dynamic range starts at cell C2 on the ListDV sheet.
动态范围的公式从ListDV表的C2单元格开始。
=ListDV!$C$2:
= ListDV!$ C $ 2:
Then, the INDEX function returns the end cell in the Emp column, based on the number in cell F1
然后,INDEX函数根据单元格F1中的数字返回Emp列中的结束单元格
INDEX(tblEmpAvail[Emp], ListDV!$F$1)
INDEX(tblEmpAvail [Emp],ListDV!$ F $ 1)
If that number changes, the end cell will adjust to the the new number.
如果该数字更改,则终止单元将调整为新数字。
修复下拉列表 (Fix the Drop Down List)
The final step is to create a drop down list that is based on the dynamic list of employees, instead of the full list of names.
最后一步是创建一个基于雇员动态列表的下拉列表,而不是姓名的完整列表。
- On the Schedule sheet, select all the cells that have the drop down list for employee names
在计划表上,选择所有具有员工姓名下拉列表的单元格
- On the Data tab of the Ribbon, click Data Validation
在功能区的“数据”选项卡上,单击“数据验证”
In the Source box, refer to the dynamic named range: =EmpListAvail
在“源”框中,引用动态命名范围: = EmpListAvail
- Click OK
点击确定
测试下拉列表 (Test the Drop Down List)
Now, test the drop down list, and it should show the short list of available employees, instead of the full list.
现在,测试下拉列表,它应该显示可用员工的简短列表,而不是完整列表。
NOTE: Some names might have previously been selected from the full list, like Al and Gil in the screen shot above. Be sure to fix those, or you might see Data Validation error warnings.
注意 :以前可能已经从完整列表中选择了一些名称,例如上面的屏幕截图中的Al和Gil。 请务必解决这些问题,否则您可能会看到“数据验证”错误警告。
获取样本文件 (Get the Sample File)
To get the sample workbook for this blog post, go to the Excel Sample Files page on my Contextures site.
要获取此博客文章的示例工作簿,请转到Contextures网站上的Excel Sample Files页面 。
In the Data Validation section, look for DV0074: Drop Down Shows Visible Items Only. The zipped file is in xlsx format, and does not contain macros.
在“数据验证”部分中,查找DV0074:“仅显示可见项目”的下拉列表。 压缩文件为xlsx格式,不包含宏。
翻译自: https://contexturesblog.com/archives/2019/01/31/drop-down-from-filtered-excel-list/
excel下拉列表数据筛选