简体   繁体   English

带有名称/值对的Excel下拉列表

[英]Excel dropdown with name/value pairs

I have workbook with 2 worksheets. 我有2个工作表的工作簿。

"Sheet2" has two columns: “Sheet2”有两列:

|    A    |      B        |
+---------+---------------+
|  code1  | description 1 |
|  code2  | Descr 2       |

Sheet1 has several columns, one of them (column D) is code . Sheet1有几列,其中一列(D列)是代码 In this column i need a "drop box", what 在这一栏中,我需要一个“投递箱”,什么

  • will show column Sheet2!B (the descriptions), and when the user selects one description 将显示列Sheet2!B(描述),以及用户选择一个描述时
  • will enter the code from the col:A. 将从col:A输入code

It is possible to do without additional helper column in Sheet1? 在Sheet1中可以不without additional helper column吗? (Excel 2010) (Excel 2010)

So, need something what is dead simple in html: 所以,在html中需要一些简单易懂的东西:

<select>
  <option value="code1">Description 1</option>
  <option value="code2">Descr 2</option>
</select>

when when the user selects "Descr 2", the form get "code2". 当用户选择“Descr 2”时,表单获得“code2”。

This question probably is an duplicate - but i'm not sure - to: How to create dropdown with multiple columns in excel , but the only answer to it pointing me to an external site where the solution is for another problem. 这个问题可能是重复的 - 但我不确定 - 如何如何在Excel中创建具有多个列的下拉列表 ,但唯一的答案是将其指向外部站点 ,其中解决方案是另一个问题。

Added a screenshot for more precise explanation: 添加了截图以获得更精确的说明: 在此输入图像描述

Simple! 简单! Here is what we are going to get! 这是我们要得到的!

在此输入图像描述

3 Steps Only: 仅限3个步骤:

  1. Define a range to use as the lookup value 定义要用作查找值的范围

  2. Create the dropdown list 创建下拉列表

  3. Paste in some code 粘贴一些代码


Step 1: Setup Sheet2 like this and define a Named Range as _descrLookup : 步骤1:像这样设置Sheet2并将命名范围定义为_descrLookup

定义VLookup的命名范围

 ( Highlight -> Right-Click -> "Define Name..." ) 

This is an optional step, but it just makes it easy to follow for Step 3. 这是一个可选步骤,但它使步骤3的操作变得简单。



Step 2: In Sheet1 , create the dropdown using Data Validation and use the VALUES YOU WANT TO BE SHOWN IN THE DROPDOWN as the source. 第2步:Sheet1 ,使用数据验证创建下拉列表,并使用您想要在DROPDOWN中显示的值作为源。 In this example it's Sheet2 A2:A4 (see above image): 在这个例子中它是Sheet2 A2:A4 (见上图):

将数据验证设置为工作表2中的源

 ( Data -> Data Validation ) 



Step 3: Add some VBA code to Sheet1 : 第3步:Sheet1添加一些VBA代码:

 ( Right-Click the tab Sheet1 -> View Code ) 

Paste this into the code window for Sheet1 : 将其粘贴到Sheet1的代码窗口Sheet1

( Right-Click the tab Sheet1 -> View Code )

It sounds like Data Validation (allow List) combined with VLOOKUP will do what you want. 听起来像数据验证(允许列表)与VLOOKUP结合将做你想要的。

On sheet 2 set up your description/code list. 在表2上设置您的描述/代码列表。 Make it a named range (helps avoid circular reference problems). 使其成为命名范围(有助于避免循环引用问题)。

On Sheet 1, in the description column, use Data Validation to make dropdown lists that reference the description column of the list. 在工作表1的说明列中,使用数据验证来生成引用列表的描述列的下拉列表。 In the code column use the VLOOKUP function, keying off of the dropdown list value. 在代码列中使用VLOOKUP函数,键入下拉列表值。

=IF(B4="", "", VLOOKUP(B4, FruitList, 2, FALSE))

Updated - 更新 -

I'm starting to see what you mean by "without a helper column", but I'm not sure you can get exactly what you want. 我开始明白你的意思是“没有辅助专栏”,但我不确定你能得到你想要的东西。 A fact of Excel design: what you see is what you get, ie the value that displays in the cell is the effective value of that cell. Excel设计的一个事实:你看到的是你得到的,即单元格中显示的值是该单元格的有效值。 You can't have a cell display one value but "contain" another value. 您不能让单元格显示一个值,但“包含”另一个值。 Such a thing is "dead simple" in HTML, but an HTML control isn't built for the same purpose as a cell in a spreadsheet. 这样的事情在HTML中是“死的简单”,但HTML控件不是为了与电子表格中的单元格相同的目的而构建的。 It's two things at the same time: a value, and a user interface presentation of that value. 它同时是两件事:一个值,以及该值的用户界面表示。 A spreadsheet cell can contain a way to determine a value (dropdown list, formula, etc.) but whatever value it reaches is going to be the value it shows. 电子表格单元格可以包含一种确定值的方法(下拉列表,公式等),但它达到的任何值都将是它显示的值。

Excel has forms support with things like combo boxes but I believe the value is still output to another cell. Excel具有组合框之类的形式支持,但我相信该值仍然输出到另一个单元格。

The usual approach to this is to use data validation to create a dropdown list and have a separate column using VLOOKUP for the code. 通常的方法是使用数据验证来创建下拉列表,并使用VLOOKUP为代码创建单独的列。 If you really can't have another column to contain the code then I'm not sure what to tell you. 如果你真的不能有另一列来包含代码那么我不知道该告诉你什么。 It would depend on how the data is to be consumed; 这取决于数据的消耗方式; are you trying to get a printout, or is the sheet being processed by another program? 你想要打印输出,还是正在由另一个程序处理的工作表?

Update 2 更新2

If you're really bent on not using a separate code column you may be able to use a combo box technique as partially described here: 如果您真的不想使用单独的代码列, 可以使用组合框技术,如下所述:

http://www.contextures.com/xlDataVal10.html http://www.contextures.com/xlDataVal10.html

It would be complicated. 这会很复杂。 What you would have to do is (a) get the combo box to show up when the user selects one of the cells in column D, and (b) dynamically adjust the box's display items. 您需要做的是(a)当用户选择D列中的一个单元格时,显示组合框,以及(b)动态调整框的显示项目。 It would involve VBA code and I'm not 100% sure it's possible. 这将涉及VBA代码,我不是100%确定它是可能的。 It certainly doesn't seem worth the effort. 这当然不值得努力。

I was able to turn on the Developer tab in Excel 2016 (15.33) for Mac OS X using the following steps: 我可以使用以下步骤在Excel 2016(15.33)中为Mac OS X启用“开发人员”选项卡:

  1. In the menu select Excel->Preferences 在菜单中选择Excel-> Preferences
  2. In the Authoring section, click View 在“ 创作”部分中,单击“ 查看”
  3. At the bottom of the dialog, check the Developer tab checkbox 在对话框的底部,选中Developer选项卡复选框

从Excel 2016(15.33)查看Mac OS X的对话框

  1. The Developer tab is now visible in Excel “开发人员”选项卡现在在Excel中可见

带有Developer选项卡的Excel主屏幕

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

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