Get the selected value of a activex combobox using

2019-07-26 00:58发布

How do i get the selected value of the combobox?

i have a combobox which has the values: "Corporate" and "Consumer".

I want to get the value that i selected, not the index, and store in a string.

something like this:

 string a = combobox.value;

(a -> Consumer)

thank you

2条回答
Emotional °昔
2楼-- · 2019-07-26 01:48

Value has a capital "V" in VBA, but assuming combobox is the name of the ComboBox you created on the screen, the code you have will work (except that your assignment statement is wrong; see below). If you don't know what the name of the ComboBox is, it is likely ComboBox1. To check, look at the Name property in the VBA properties window.

Try this:

Dim a as String

a = combobox.Value
查看更多
Rolldiameter
3楼-- · 2019-07-26 01:55

If your ComboBox is embedded in a spreadsheet you can use this:

Dim ws as Worksheet
Dim cboCorpConsumer as ComboBox
Dim a as String

Set ws = Worksheets("YourWorksheetName")
Set cboCorpConsumer = ws.OLEObjects("cboNameFromActiveXProperties").Object

a = cboCorpConsumer.Value

Or in one line:

a = Worksheets("YourWorksheetName").OLEObjects("cboNameFromActiveXProperties").Object.Value
查看更多
登录 后发表回答