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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:20:51  来源:igfitidea点击:

numeric up down control in vba

excelvbaexcel-2010numericupdown

提问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 SpinButton1control for that

您可以使用该SpinButton1控件

SNAPSHOT

快照

enter image description here

在此处输入图片说明

CODE

代码

You can either set the min and max of the SpinButton1in 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