文本和值组合框 Excel VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17597710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:09:53  来源:igfitidea点击:

Text and value to combobox Excel VBA

excelvbaexcel-vbacombobox

提问by lealam

I have two tables in Excel
First Table: Table Students: ID, last name, first name address and etc. Table Two: Notes on the students: each student can have some records,i have two columns of this table. student ID and comments The table I want to fill out a form
Will form a combo box with all the students and a text box for entering notes, the Add button will add the records
I wanted a combo box will appear the name of the student (last name+first name) and when they click Add, the code will put the ID student's of the selected student

我在Excel
First Table中有两个表格:表格学生:ID,姓氏,名字地址等。 表格二:学生注意事项:每个学生可以有一些记录,我有这个表格的两列。学生ID和评论表格我要填写表格
会形成一个包含所有学生的组合框和一个用于输入笔记的文本框,添加按钮将添加
我想要的记录一个组合框会出现学生的姓名(姓氏+名字),当他们点击添加时,代码将放置所选学生的学生ID

I thought Combo Box can be set for each row: text and value, the text view and the value is not

我以为可以为每一行设置组合框:文本和值,文本视图和值不是

As in ASP.NET

就像在 ASP.NET 中一样

dropdownlist.datatextfield = Name
dropdownlist.datavaluefield = ID

Now I see only text can be set
Am I wrong and I can not well known, and if not how can I solve the problem

现在我只看到可以设置文本
是我错了,我不知道,如果不是我该如何解决问题

回答by lealam

you have declare two combo box
one with the names and second with rhe id
the id's combo box there is disables
Order of the two combobox will be listed as the order in which it appears in the table

To get the ID of the selected student:

你已经声明了两个组合框,
一个是名字,第二个
是 id id 的组合框有禁用
两个组合框的顺序将作为它在表中出现的顺序列出

来获取所选学生的 ID:

cbxId.List(cbxNames.ListIndex)

回答by Paul Garbutt

You can create a ComboBox with 2 columns - value and text.

您可以创建一个包含 2 列的 ComboBox - 值和文本。

In Excel, define the 2-column range and then use that as the rowsource for the control.

在 Excel 中,定义 2 列范围,然后将其用作控件的行源。

In the combobox properties, use BoundColumn = 1and ColumnCount = 2, with the ColumnWidthsvalues = 0pt; 20pt.

在组合框属性中,使用BoundColumn = 1ColumnCount = 2ColumnWidths值为 = 0pt; 20pt

This mimicks the classic HTML 'select' control quite well I find, as you can simply call the Value from the control and as you've set the bound column to be the 'value' column, then it returns your ID for you quite easily.

我发现这很好地模仿了经典的 HTML 'select' 控件,因为您可以简单地从控件中调用 Value 并且您已将绑定列设置为“值”列,然后它很容易为您返回您的 ID .

combo box properties

组合框属性

results when cmd is clicked

单击 cmd 时的结果

raw data

原始数据