excel下拉菜单vba
With Excel's data validation, you can show a drop down list of items in a cell. You can even create "dependent" drop downs. For example, select a region, and see only the customers in that region. See how to show a warning in Excel drop down list, if the source data is not set up correctly.
使用Excel的数据验证,您可以在单元格中显示项目的下拉列表。 您甚至可以创建“从属”下拉列表。 例如,选择一个区域,然后仅查看该区域中的客户。 如果源数据设置不正确,请参见如何在Excel下拉列表中显示警告。
从属下拉列表 (Dependent Drop Down Lists)
In this example, select a region name in column B. Then, when you click the drop down arrow in column C, the list just shows the customers in that region.
在此示例中,在B列中选择一个区域名称。然后,当您单击C列中的下拉箭头时,列表仅显示该区域中的客户。
There are a couple of benefits to dependent drop down lists:
依赖下拉列表有几个好处:
- It's easier to pick a customer from a short list, instead of the full list 从短列表而不是完整列表中选择客户比较容易
- It encourages valid customer entries. (Data validation isn't bulletproof – there are ways to get around it) 它鼓励有效的客户输入。 (数据验证不是防弹的,有很多方法可以解决)
Get the sample file, and see the complete setup instructions on my website.
名单 (The Lists)
There are two lists used in this dependent data validation technique.
在此依赖数据验证技术中使用了两个列表。
- The Region drop down is based on the Regions list. 区域下拉列表基于“区域”列表。
- The Customer drop down shows the customers for the selected region, from the Region/Customer table “客户”下拉列表从“地区/客户”表中显示所选地区的客户
客户下拉 (Customer Drop Down)
The dependent drop down for Customer uses OFFSET, MATCH and COUNTIF to find the customers for the selected region.
客户的从属下拉列表使用OFFSET,MATCH和COUNTIF查找所选区域的客户。
=OFFSET(RegionStart, MATCH(B2,RegionColumn,0)-1, 1, COUNTIF(RegionColumn,B2),1)
= OFFSET(RegionStart,MATCH(B2,RegionColumn,0)-1,1,COUNTIF(RegionColumn,B2),1)
It finds the first instance of the region name, and gets customers from the next column, based on a count of the region name. In this screen shot, East is in the 8th row, and the six customers from that region would appear in the drop down.
它查找区域名称的第一个实例,并根据区域名称的计数从下一列获取客户。 在此屏幕快照中,East在第8行,该区域的六个客户将出现在下拉列表中。
See my website, for more details on how the formula works.
按地区排序 (Sort By Region)
For the OFFSET formula to work correctly, the lookup table MUST be sorted by the Region column. If the list is sorted by customer name, the East region list would show the wrong set of 6 names.
为了使OFFSET公式正常工作,必须按Region列对查找表进行排序。 如果该列表按客户名称排序,则东部地区列表将显示错误的6个名称集。
检查区域是否已排序 (Check if the Regions Are Sorted)
In the original version of this technique, you had to remember to sort the list by region, after making any changes to the lookup list. There wasn't a warning system to alert you to problems.
在此技术的原始版本中,您必须记住在对查找列表进行任何更改之后,按区域对列表进行排序。 没有警告系统可以提醒您问题。
To help avoid errors, I've created a new sample file, and it has formulas to check if the region names are in A-Z order.
为了避免发生错误,我创建了一个新的示例文件,该文件具有用于检查区域名称是否按AZ顺序排列的公式。
There's a new column (SortCheck) in the lookup table, with a formula to check the order.
查找表中有一个新列(SortCheck),其中包含用于检查顺序的公式。
=IF(A3="",0,--(A3<A2))
= IF(A3 =“”,0,-(A3 <A2))
If an item is out of order, there is a 1 in the row above it. The "East" in A5 is less than the "West" in A4, so cell C4 returns a 1, instead of a zero.
如果某件商品出现故障,则该商品上方的行中有1。 A5中的“东部”小于A4中的“西部”,因此单元格C4返回1,而不是零。
获取总数 (Get the Total Number)
In a cell named SortCheck, a formula calculates the total for that SortCheck column.
在名为SortCheck的单元格中,公式将计算该SortCheck列的总数。
=SUM(tblRegCust[SortCheck])
= SUM(tblRegCust [SortCheck])
Another named cell, SortMsg, contains a typed error message that will be used in the data validation.
另一个命名单元SortMsg包含将在数据验证中使用的类型化错误消息。
在Excel下拉菜单中显示警告 (Show Warning in Excel Drop Down)
To show a warning in Excel drop down when necessary, I changed the Customer data validation formula slightly. The IF function looks at the total, and shows the SortMsg range, if the total is greater than zero.
为了在必要时在Excel下拉菜单中显示警告,我略微更改了客户数据验证公式。 如果总数大于零,则IF函数查看总数,并显示SortMsg范围。
=IF(SortCheck>0, SortMsg, OFFSET(RegionStart,MATCH(B2,RegionColumn,0)-1, 1, COUNTIF(RegionColumn,B2),1))
= IF(SortCheck> 0,SortMsg, OFFSET(RegionStart,MATCH(B2,RegionColumn,0)-1,1,COUNTIF(RegionColumn,B2),1)))
You'll have to fix the list, before you can choose a customer name.
您必须先修正列表,然后才能选择客户名称。
It's easy to overlook a message that's in a worksheet cell, but this error message is hard to ignore!
忽略工作表单元格中的消息很容易,但是很难忽略该错误消息!
其他相关方法 (Other Dependent Methods)
For other ways to create dependent drop down lists, go to the following pages on my Contextures site:
有关创建依赖下拉列表的其他方法,请转到Contextures网站上的以下页面:
获取样本文件 (Get the Sample File)
Get the warning in Excel drop down sample file, and see the complete instructions on my website.
在Excel下拉示例文件中获得警告 ,并在我的网站上查看完整说明。
翻译自: https://contexturesblog.com/archives/2018/02/08/show-warning-in-excel-drop-down/
excel下拉菜单vba