如何在Excel VBA中控制TextBox的KeyPress方法

时间:2018-12-10 07:55:56

标签: excel vba excel-vba textbox keypress

我有一个代码可以在多页内创建文本框:

Private Sub CommandButton1_Click()

RowChar = 70
MultiPage1.Pages.Clear

For i = 0 To TextBox1.Value - 1
    MultiPage1.Pages.Add
    MultiPage1.Pages(i).Caption = "Variable" & i + 1

    Call LabelPerPage

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "NameBox")
    With txtbx
        .Top = 20
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MinBox")
    With txtbx
        .Top = 50
        .Left = 100
    End With


    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "LsbBox")
    With txtbx
        .Top = 20
        .Left = 300
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MaxBox")
    With txtbx
        .Top = 50
        .Left = 300
    End With

    If i = 0 Then
        FormulaString = "= C15"
    Else
        FormulaString = FormulaString & "  " & Chr(RowChar) & "15"
        RowChar = RowChar + 3
    End If
Next i
TextBox2.Value = FormulaString
End Sub

Private Sub LabelPerPage()
    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 20
        .Left = 50
        .Caption = "NAME:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 50
        .Left = 50
        .Caption = "MIN:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 20
        .Left = 250
        .Caption = "LSB:"
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.Label.1")
    With txtbx
        .Top = 50
        .Left = 250
        .Caption = "MAX:"
    End With
End Sub

我试图创建一个可以在其中创建页面和文本框的代码,我的问题是我无法对TextBox进行KeyPress,因为由于我的代码它只会自动创建。

目标:

1。)我的目标是在TextBox无法输入数字值或Letter的情况下进行KeyPress。

2。)我想比较2个文本框,其中textbox1至少应为textbox2

**我希望有人可以帮助我,因为我已尽力解决了它。我尝试对代码执行此操作:

选项1:

Private Sub MaxBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 46 And KeyAscii < 58) Or KeyAscii = 43 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
MsgBox "Invalid key pressed, you can enter numbers only"
End If
End Sub

选项2:

Private Sub OnlyNumbers()
    If TypeName(Me.ActiveControl) = "MaxBox" Then
        With Me.ActiveControl
            If Not IsNumeric(.Value) And .Value <> vbNullString Then
                MsgBox "Sorry, only numbers allowed"
                .Value = vbNullString
            End If
        End With
    End If
End Sub

但是它不起作用。预先感谢您是否可以提供帮助:)

1 个答案:

答案 0 :(得分:1)

为什么不完全失去keyPress,因为您要实现的唯一目标就是仅数字输入?您可以在用户表单代码中执行以下操作:

Option Explicit
'Variable to capture Change event from your textbox:
Private WithEvents maxbox As MSForms.TextBox

'The creation of the thing; I just created a multipage control to reuse your lines.
Private Sub UserForm_Initialize()
    Dim txtbox as MSForms.TextBox
    Dim i As Integer
    i = 0
    Set txtbox = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MaxBox")
    With txtbox 
        .Top = 50
        .Left = 50
    End With
    Set maxbox = txtbox
End Sub

'Capture change of maxbox:
Private Sub maxbox_Change()
    'In case somebody entered something non-numeric:   
    If IsNumeric(maxbox.Text) = False Then
        'Remove the input character
        maxbox.Text = Left(maxbox.Text, Len(maxbox.Text) - 1)
        'And alert the user
        MsgBox "numeric only!"
    End If
End Sub

如果需要多个,也可以只创建一个捕获事件的自定义类,并将该类的collection添加到用户窗体中。为此,您可以查看答案on this question

编辑:对于下半部分(针对Minbox进行验证),您可以使用相同的事件:只需添加另一个if语句以确保数值为> CInt(minbox.text)(或其他数字类型。

编辑2:,如果字符串的长度为0(例如,当有人按下Backspace键/删除键以触发更改事件时),您可能想为Left(maxbox.Text, Len(maxbox.Text) - 1)位添加错误处理)。