vba 为excel VBA中的组合框赋值

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

Assigning values to Combo box in excel VBA

excelvbaexcel-vbacomboboxlistbox

提问by iliketocode

I am trying to set the values of the combo box to 0, 1, and 2 (hard coded values and not a cell reference) but it does not seem to work. I have tried setting the RowSourceType" to 1 and toValue List`, but I am getting compile errors every time. For example, the following code does not work:

我试图将组合框的值设置为 0、1 和 2(硬编码值而不是单元格引用),但它似乎不起作用。我曾尝试设置RowSourceType" to 1 and to值列表`,但每次都会出现编译错误。例如,以下代码不起作用:

Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = "Value List"
Me.errorComboBox.RowSource = "0;1;2"
End Sub

or

或者

Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = 1
Me.errorComboBox.RowSource = "0;1;2"
End Sub

I am getting errors on the RowSourceTypeline for both. How can I do this?

我在RowSourceType这两个方面都遇到了错误。我怎样才能做到这一点?

回答by djikay

To statically populate the list of a combo box in Excel, try something like this:

要静态填充 Excel 中的组合框列表,请尝试以下操作:

Me.errorComboBox.List = Array("0", "1", "2")

This SO questioncontains links and other examples to achieve what you want.

这个 SO 问题包含链接和其他示例来实现您想要的。