vba vba中的数字上下控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11116808/
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
numeric up down control in vba
提问by Premanshu
Is there an in-built numeric updown control in vba or do we need to create a control like that?
vba 中是否有内置的数字上下控件,还是我们需要创建这样的控件?
If there is such a control then what are the events that we may use.
如果有这样的控件,那么我们可以使用哪些事件。
Pls suggest.
请建议。
回答by Siddharth Rout
You can use the SpinButton1
control for that
您可以使用该SpinButton1
控件
SNAPSHOT
快照
CODE
代码
You can either set the min and max of the SpinButton1
in design time or at runtime as shown below.
您可以设置SpinButton1
设计时或运行时的最小值和最大值,如下所示。
Private Sub UserForm_Initialize()
SpinButton1.Min = 0
SpinButton1.Max = 100
End Sub
Private Sub SpinButton1_Change()
TextBox1.Text = SpinButton1.Value
End Sub
FOLLOWUP
跟进
If you want to increase or decrease the value of the textbox based on what user has input in the textbox then use this. This also makes the textbox a "Number Only" textbox which just fulfills your other request ;)
如果您想根据用户在文本框中输入的内容来增加或减少文本框的值,请使用它。这也使文本框成为“仅数字”文本框,它仅满足您的其他请求;)
Private Sub SpinButton1_SpinDown()
TextBox1.Text = Val(TextBox1.Text) - 1
End Sub
Private Sub SpinButton1_SpinUp()
TextBox1.Text = Val(TextBox1.Text) + 1
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case vbKey0 To vbKey9, 8
Case Else
KeyAscii = 0
Beep
End Select
End Sub