Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the selected value of a activex combobox using vba

Tags:

excel

vba

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

like image 667
Raphael Avatar asked Dec 21 '22 02:12

Raphael


2 Answers

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
like image 177
mankind_nyc Avatar answered Feb 27 '23 02:02

mankind_nyc


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
like image 27
Michael Kingsmill Avatar answered Feb 27 '23 03:02

Michael Kingsmill