vba 在没有代码的情况下将项目添加到组合框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2997876/
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
Adding Items To A Combo Box Without Code?
提问by sooprise
I want some default values in my combo boxes but can't seem to figure out how to do this without writing a module that populates the combo boxes. How can I manually fill in the combo boxes so I don't have to use code to do something so simple.
我想在我的组合框中使用一些默认值,但似乎无法弄清楚如何在不编写填充组合框的模块的情况下执行此操作。我如何手动填写组合框,这样我就不必使用代码来做这么简单的事情。
Thanks
谢谢
采纳答案by Hans Olsson
If I understand you question correctly, I'm fairly sure you have to have code somewhere, I don't think VBA supports as I seem to remember that VB6 did to just add the items in a property, but you don't have to create a separate module, just something like this should work:
如果我正确理解你的问题,我很确定你必须在某处有代码,我认为 VBA 不支持,因为我似乎记得 VB6 只是在属性中添加项目,但你不必创建一个单独的模块,就像这样应该可以工作:
Private Sub UserForm_Activate()
ComboBox1.AddItem "Text1"
ComboBox1.AddItem "Text2"
ComboBox1.AddItem "Text3"
End Sub
回答by Patrick Honorez
I guess you speak about Access ? If that's the case,
我猜你说的是 Access ?如果是这样的话
- set the Row Source Type to "Value List"
- then set the Row Source to Black; Blue; Green or whatever you want
- 将行源类型设置为“值列表”
- 然后将行源设置为黑色;蓝色; 绿色或任何你想要的
回答by Jay
What program are you using? Where is the combobox. For example, in Excel, you can have a combobox on a form or on the worksheet, and the answer is going to depend on these factors.
你在用什么程序?组合框在哪里。例如,在 Excel 中,您可以在表单或工作表上有一个组合框,答案将取决于这些因素。
In Excel, you can define your default values in a worksheet (which you can hide, if you want), and set the combo box source to the corresponding range -- no code required.
在 Excel 中,您可以在工作表中定义默认值(如果需要,您可以隐藏它),并将组合框源设置为相应的范围——无需代码。
Please elaborate on your requirements and you'll get more specific answers.
请详细说明您的要求,您将得到更具体的答案。
回答by Jayson
You could also try something like
你也可以尝试类似的东西
Private Sub worksheet_Activate()
私有子工作表_Activate()
With Sheet1.ComboBox1 .List = Range("a1:a20").Value End With 'This will give a list of the values from Cell A1 to Cell A20 End Sub
With Sheet1.ComboBox1 .List = Range("a1:a20").Value End With '这将给出从单元格 A1 到单元格 A20 End Sub 的值列表