VBA中的数字上向下控制 [英] numeric up down control in vba

查看:306
本文介绍了VBA中的数字上向下控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vba中是否有内置的数字上下控件,还是我们需要创建类似的控件?

Is there an in-built numeric updown control in vba or do we need to create a control like that?

如果有这样的控件,那么我们可以使用哪些事件.

If there is such a control then what are the events that we may use.

建议.

推荐答案

您可以为此使用SpinButton1控件

快照

代码

您可以在设计时或在运行时设置SpinButton1的最小值和最大值,如下所示.

You can either set the min and max of the SpinButton1 in design time or at runtime as shown below.

Private Sub UserForm_Initialize()
    SpinButton1.Min = 0
    SpinButton1.Max = 100
End Sub

Private Sub SpinButton1_Change()
    TextBox1.Text = SpinButton1.Value
End Sub

关注

如果要基于用户在文本框中输入的内容来增加或减少文本框的值,请使用此选项.这也使文本框成为仅满足您其他要求的仅数字"文本框;)

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

这篇关于VBA中的数字上向下控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆