简体   繁体   中英

Text and value to combobox Excel VBA

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

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

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

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:

cbxId.List(cbxNames.ListIndex)

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

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

In the combobox properties, use BoundColumn = 1 and ColumnCount = 2 , with the ColumnWidths values = 0pt; 20pt 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.

combo box properties

results when cmd is clicked

raw data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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