使用 vba 获取 activex 组合框的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9082962/
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-08 12:30:23  来源:igfitidea点击:

Get the selected value of a activex combobox using vba

excelvbaexcel-vba

提问by Raphael

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)

(a -> 消费者)

thank you

谢谢你

回答by mankind_nyc

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

如果您的 ComboBox 嵌入在电子表格中,您可以使用:

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

回答by Michael Kingsmill

Valuehas a capital "V" in VBA, but assuming comboboxis 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 Nameproperty in the VBA properties window.

Value在 VBA 中有一个大写的“V”,但假设combobox是您在屏幕上创建的 ComboBox 的名称,您拥有的代码将起作用(除了您的赋值语句是错误的;见下文)。如果您不知道 ComboBox 的名称是什么,则很可能是ComboBox1. 要检查,请查看NameVBA 属性窗口中的属性。

Try this:

尝试这个:

Dim a as String

a = combobox.Value