要从下拉列表中选择的 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28015878/
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
Excel VBA to select from drop-down list
提问by amymon87
I have written a VBA Macro in excel which creates spreadsheets from a template and updates values for individual spreadsheets based on options in a drop down list.
我在 excel 中编写了一个 VBA 宏,它从模板创建电子表格,并根据下拉列表中的选项更新单个电子表格的值。
I cant seem to figure out how to use VBA to choose the first option on the list.
我似乎无法弄清楚如何使用 VBA 来选择列表中的第一个选项。
Any help appreciated.
任何帮助表示赞赏。
回答by Matteo NNZ
Say your drop down list is named myList
, the way to select items programmatically is by using the .ListIndex
property of the object. Which means:
假设您的下拉列表名为 named myList
,以编程方式选择项目的方法是使用.ListIndex
对象的属性。意思是:
myList.ListIndex = j
where j goes from 0
(first element) to n-1
(last element). In your case:
其中 j 从0
(第一个元素)到n-1
(最后一个元素)。在你的情况下:
myList.ListIndex = 0
will select the first item.
将选择第一项。
IF THE CONTROL IS IN THE SPREADSHEET:
如果控制在电子表格中:
in that case the code is slightly different:
在这种情况下,代码略有不同:
With Sheets("Sheet1").Shapes("Region").ControlFormat
.ListIndex = 0
End With