vba 如何使用VBA获取Excel电子表格中组合框的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3233733/
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
How to get the index of a combo box in Excel spreadsheet by using VBA?
提问by Haris
I have an Excel 2003 file. In the first sheet, I put a combo box. After that, I assign a macro to handle the change event. The macro is located a module in the VB Editor's Project Explorer box. In the macro, I want to write some lines to get the index of the combo box's selected item index. What should I write?
我有一个 Excel 2003 文件。在第一张表中,我放了一个组合框。之后,我分配一个宏来处理更改事件。该宏位于 VB 编辑器的项目资源管理器框中的一个模块中。在宏中,我想写一些行来获取组合框的选定项索引的索引。我应该写什么?
I wrote the line
我写了一行
If ActiveSheet.cbFilter.Index = 1 Then
but it kept raising error (the name of the combo box is "cbFilter"). The error message is "Object doesn't support this property of method". Please help me.
但它不断引发错误(组合框的名称是“cbFilter”)。错误消息是“对象不支持方法的此属性”。请帮我。
Many thanks in advance,
提前谢谢了,
Haris
哈里斯
回答by Ciago
Try removing the "ActiveSheet." part of the statement, my guessing is the combo is already inside that sheet.
尝试删除“ActiveSheet”。声明的一部分,我的猜测是组合已经在该表中。
Source: Experience
资料来源:经验
Regards
问候
回答by Santosh
Try this. If you look into Object brower cbFilter
object is property of sheet1
and can be referred as below from module.
尝试这个。如果您查看对象浏览器cbFilter
对象是属性的sheet1
并且可以从模块中引用如下。
MsgBox Sheet1.cbFilter.ListIndex
If Sheet1.cbFilter.ListIndex = 1 Then
回答by MikeD
have you added items to your combo list?
您是否已将项目添加到组合列表中?
try these pieces of code in your debugger: first call Preload() once, then select anything from the combo, then run Readout() .... this should give you a hint.
在调试器中尝试这些代码:首先调用 Preload() 一次,然后从组合中选择任何内容,然后运行 Readout() .... 这应该给你一个提示。
Sub Preload()
ActiveSheet.ComboBox1.AddItem "111"
ActiveSheet.ComboBox1.AddItem "222"
ActiveSheet.ComboBox1.AddItem "333"
End Sub
Sub ReadOut()
ActiveSheet.[A1] = ActiveSheet.ComboBox1.ListIndex
ActiveSheet.[A2] = ActiveSheet.ComboBox1
End Sub
also you should check that you have created a reference to MSForms 2.0 Object library (FM20.DLL - or similar)
您还应该检查您是否已经创建了对 MSForms 2.0 对象库(FM20.DLL - 或类似的)的引用
EDIT:
编辑:
I just tested the case of empty Combo ... index will be -1
我刚刚测试了空 Combo 的情况......索引将为 -1