使用 VBA 为 Excel 中的组合框提供默认选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18789532/
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
give a combobox in Excel a default selected value with VBA
提问by Sam
I have a combobox in Excel created as the picture suggests, and it has a range associated to it.
如图所示,我在 Excel 中创建了一个组合框,它有一个与之关联的范围。
How can I give it a default value from this range, through VBA?
如何通过 VBA 为其提供此范围内的默认值?
ActiveSheet.Shapes("DropDown1").ControlFormat.Value = "Germany"
I tried the above code, but it doesn't work. I believe the syntax is not correct. Can you please help?
Thank you!
我试过上面的代码,但它不起作用。我认为语法不正确。你能帮忙吗?谢谢!
回答by ddfxraven
You can use the DropDown type, it doesn't show up in the intellisence when you type.
您可以使用 DropDown 类型,它不会在您键入时显示在智能中。
Dim DropDown1 As DropDown
Set DropDown1 = ActiveSheet.DropDowns("DropDown1")
DropDown1.Value = 1
The value is the index of the dropdown, so 1 is the first in the list etc. Use 0 to have no value selected. Also make sure the name "DropDown1" is the correct name for the control, if you right click the control in the excel sheet you'll see the name for the control to the left of the function bar.
该值是下拉列表的索引,因此 1 是列表中的第一个等。使用 0 表示没有选择任何值。还要确保名称“DropDown1”是控件的正确名称,如果您右键单击 Excel 工作表中的控件,您将在功能栏的左侧看到控件的名称。