简体   繁体   English

如何为按键事件制作子项?

[英]How can i make a sub for a keypress event?

I'm working on an excel with macros. 我正在使用宏的Excel。 I have a userform with textboxes, several of them use a function for only press numbers. 我有一个带有文本框的用户窗体,其中有几个使用仅用于按数字的功能。

用户表格示例

Private Sub quantity1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
        KeyAscii = 0
    End Select
End Sub

How can i put the select case on a procedure or function and then call it from any keypress event that i need it (in this case, quantity1 quantity2, price1 and price2, but no in buyer)? 如何将选择用例放在过程或函数上,然后从我需要的任何按键事件中调用它(在这种情况下,为quantity1quantity2,price1和price2,但在买方中则没有)? I tried making a sub that uses the same parameters like the event eg: 我尝试制作一个使用与事件相同的参数的子,例如:

Sub Only_Numbers(ByVal KeyAscii As MSForms.ReturnInteger)

Here is a quick example that I created for you. 这是我为您创建的一个简单示例。

Let's say your userform looks like this 假设您的用户表单如下所示

在此处输入图片说明

Now place this in a class module 现在将其放在一个类模块中

Public WithEvents TextBoxEvents As MSForms.TextBox

Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
            KeyAscii = 0
    End Select
End Sub

Screenshot 屏幕截图

在此处输入图片说明

And place this in the userform 并将其放在用户表单中

Dim myTBs() As New Class1

Private Sub UserForm_Initialize()
    Dim i As Integer, objControl As Control

    For Each objControl In Me.Controls
        If TypeOf objControl Is MSForms.TextBox Then
            i = i + 1
            ReDim Preserve myTBs(1 To i)
            Set myTBs(i).TextBoxEvents = objControl
        End If
    Next objControl
    Set objControl = Nothing
End Sub

Screenshot 屏幕截图

在此处输入图片说明

Now try entering text/numbers in any of the textboxes :) 现在尝试在任何文本框中输入text/numbers :)

EDIT 编辑

@SiddharthRout Wow! @SiddharthRout哇! Awesome Reply! 很棒的回复! it's almost like that, but i require that certain textboxes (not all) on the userform could have this restriction. 几乎就像那样,但是我要求用户窗体上的某些文本框(不是全部)可以有此限制。 I guess that if i do this Set myTBs(i).TextBoxEvents = quantity1 (Considering that quantity1 is a textbox) should work? 我想如果我这样做Set myTBs(i).TextBoxEvents = Quantity1(考虑quantity1是文本框)应该可以工作? – fjatp 6 mins ago – fjatp 6分钟前

        If TypeOf objControl Is MSForms.TextBox Then
            Select Case objControl.Name
            Case "TextBox1", "TextBox3", "TextBox4" '<~~ Include only these
                i = i + 1
                ReDim Preserve myTBs(1 To i)
                Set myTBs(i).TextBoxEvents = objControl
            End Select
        End If

As a slight modification of Siddharth's code I use collections instead of an array. 作为对Siddharth代码的略微修改,我使用集合而不是数组。 Anything else remains the same. 其他都保持不变。

Option Explicit

Dim myTBs As New Collection

Private Sub UserForm_Initialize()
    Dim i As Integer, objControl As Control
    Dim TB As Class1

    For Each objControl In Me.Controls
        If TypeOf objControl Is MSForms.TextBox Then
            Set TB = New Class1
            Set TB.TextBoxEvents = objControl
            myTBs.Add TB
        End If
    Next objControl
    Set objControl = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM