vba 从excel vba的下拉列表中选择特定项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22232521/
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
Selecting a specific item from Dropdown in excel vba
提问by jDave1984
I'm working out a dashboard for my office. It all works, but I wanted to add in an option that instead of searching through 250+ items in a dropdown, you could also click on a cell and the dropdown would change to that value, and the assigned macro would run for that dropdown. So far I can't figure out how to have vba select a specific item from a dropdown. I can have the text change, but that doesn't select the index of the dropdown.
我正在为我的办公室设计一个仪表板。一切正常,但我想添加一个选项,而不是在下拉列表中搜索 250 多个项目,您还可以单击一个单元格,下拉列表将更改为该值,并且分配的宏将为该下拉列表运行。到目前为止,我无法弄清楚如何让 vba 从下拉列表中选择特定项目。我可以更改文本,但这不会选择下拉列表的索引。
Any suggestions?
有什么建议?
回答by Pedrumj
If you are using an ActiveX drop down list, (a combobox) this is how you would change the selected value in the drop down list:
如果您使用的是 ActiveX 下拉列表(组合框),您可以通过以下方式更改下拉列表中的选定值:
ComboBox1.Value = "New Value"
If you are using data validation as a drop down list, then you just need to change the cell value as you would change any other cell:
如果您使用数据验证作为下拉列表,那么您只需要像更改任何其他单元格一样更改单元格值:
'assuming the drop down list (data validation) is in Cell(1, 1)
cells(1,1) = "New Value"
Also you could check this article I've written on my blog about working with drop down lists in VBA Excel VBA Drop Down Lists
您也可以查看我在博客上写的有关在 VBA Excel VBA 下拉列表中使用下拉列表的文章
回答by Eidolon
I wanted to do the same: Select an option from several drop down lists, then "reset" all of them to a starting point. Recording a macro and change the drop down list wont work.
我想做同样的事情:从几个下拉列表中选择一个选项,然后将它们全部“重置”到一个起点。录制宏并更改下拉列表不起作用。
However, I did the following:
但是,我做了以下事情:
- Start recording macro
- I typed all the values I wanted to be the "starting point once reset on the drop down list
- stop macro.
- 开始录制宏
- 我在下拉列表中输入了我想成为“重置后的起点”的所有值
- 停止宏。
It worked. Now I hit the macro and I get all the values "reset". Cheap trick, I know but worked for me.
有效。现在我点击了宏,我得到了“重置”的所有值。便宜的把戏,我知道但对我有用。