Excel - 通过 VBA 显示 ComboBox DropDown
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13040694/
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 - Display ComboBox DropDown by VBA
提问by Shorn Jacob
I need a workbook to display the Combobox List Dropdown when it opens.
我需要一个工作簿来在打开时显示组合框列表下拉列表。
The combobox in the Workbook is a form control, so a shape.
工作簿中的组合框是一个表单控件,因此是一个形状。
Cant seem to find the associated property.
似乎无法找到相关的财产。
回答by Siddharth Rout
If you are using ActiveX Controls then see the below, else if you are using Form Controls then replace them with ActiveX Controls if you want the drop down to happen via code. The below code will work for both ActiveX Controls in a Form as well as Worksheet. If the Control is on a worksheet then change ComboBox1.SetFocus
to ComboBox1.Activate
如果您使用的是 ActiveX 控件,请参阅以下内容,否则,如果您使用的是表单控件,则如果您希望通过代码进行下拉,则将它们替换为 ActiveX 控件。下面的代码适用于表单和工作表中的 ActiveX 控件。如果控件在工作表上,则更ComboBox1.SetFocus
改为ComboBox1.Activate
Two ways I can think of...
我能想到的两种方法...
- Using a simple command
- 使用简单的命令
Tried And Tested
久经考验
Private Sub CommandButton1_Click()
ComboBox1.DropDown
End Sub
- Using Sendkeys. Sendkeys are unreliable if not used properly.
- 使用发送键。如果使用不当,发送键是不可靠的。
Tried And Tested
久经考验
Private Sub CommandButton1_Click()
ComboBox1.SetFocus
SendKeys "%{Down}"
End Sub
SCREENSHOTS
截图
回答by Harry S
I have had plenty of crashes with .dropdown but find some success with the SendKeys...
我在使用 .dropdown 时遇到过很多崩溃,但使用 SendKeys 找到了一些成功...
回答by Harry S
I consider best UserForm combo box is as Above by Siddharth Rout ComboBox1.SetFocus SendKeys "%{Down}"
我认为最好的 UserForm 组合框是 Siddharth Rout ComboBox1.SetFocus SendKeys "%{Down}"
for some Combo boxes on a worksheets CB.DropDown Is enough .. Just as well as they have no setfocus or activate
对于工作表上的某些组合框 CB.DropDown 就足够了 .. 就像它们没有 setfocus 或 activate