输入函数作为组合框 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21585759/
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
Input function as a Combo Box VBA
提问by Siddharth Rout
I am building a macro that will run then pause the macro and allow the user to input a value then continue running again. I know there is InputBox function available but I want the box to pop up to be a drop down list. I am not sure how to do this, any help would be greatly appreciated.
我正在构建一个宏,它将运行然后暂停宏并允许用户输入一个值然后再次继续运行。我知道有可用的 InputBox 功能,但我希望该框弹出为下拉列表。我不知道如何做到这一点,任何帮助将不胜感激。
回答by Siddharth Rout
Siddharth thank you for the response! would you mind showing me a example of what the code would look like – fishing king 13 6 mins ago
悉达多感谢您的回复!你介意给我看一个代码是什么样子的例子 - 钓鱼王 13 6 分钟前
Hope this gets you on the right track...
希望这能让你走上正轨......
Add a Userform and add a Combobox to it. I am adding some basic data in the combo to show you how it works. Change as applicable
添加一个用户表单并向其添加一个组合框。我在组合中添加了一些基本数据,以向您展示它是如何工作的。适用时更改
Your Userform will have a code something like this
您的用户表单将有一个类似这样的代码
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "option1"
.AddItem "option2"
.AddItem "option3"
.AddItem "option4"
.AddItem "option5"
End With
End Sub
Private Sub CommandButton1_Click()
If ComboBox1.Text <> "" Then MsgBox "the user chose or typed " & ComboBox1.Text
End Sub
Next amend your macro so that it looks like this
接下来修改您的宏,使其看起来像这样
Sub Sample()
'
'~~> Do Some Stuff
'
UserForm1.Show
'
'~~> Continue doing Some Stuff
'
End Sub
Followup from comments
来自评论的跟进
One more question If the value of my drop down come from a named range. How do I define that? – fishing king 13 3 mins ago
另一个问题如果我的下拉列表的值来自命名范围。我该如何定义?– 钓鱼王 13 3 分钟前
Use .List
instead of .AddItem
使用.List
代替.AddItem
Private Sub UserForm_Initialize()
ComboBox1.List = Application.Transpose(Range("MyNamedRange"))
End Sub