excelvbatextboxuserform

How to limit user to input Numeric number only in all Textbox of Excel userform


I have 60 Textbox in a Excel Userform. To limit user to input numeric number(Decimal) only for TextBox1 to TextBox50, I need to write lots of same code as below.

My question:

1.I want to create a class/function whatever as if I don't need to write same code for TextBox1 to TextBox50. Is there easy solution?

2.If i want to limit user for numeric number in All textbox of userform.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 46 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'same code
End Sub

    .......
    .......

Solution

  • Try the next way, please:

    1. Insert a class module as a event wrapper class and name it "TxtBClass", then copy the next code in its module:
    Option Explicit
    
    Public WithEvents txtBEvent As MSForms.TextBox
    
    
    Private Sub txtBEvent_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 46 Then
          KeyAscii = KeyAscii
      Else
          KeyAscii = 0
      End If
    End Sub
    
    1. Paste the next code in a standard module:
    Option Explicit
    
    Private txtB() As New TxtBClass
    
    Sub AssignTxtBoxEvent()
      Dim ws As Worksheet, k As Long, oObj As OLEObject
      
      Set ws = ActiveSheet 'use here your necessary sheet
      ReDim txtB(100) 'maximum text boxes to be processed (can be increased)
      
      For Each oObj In ws.OLEObjects
        If TypeName(oObj.Object) = "TextBox" Then
            'exclude the textboxes you need to be excluded from this common event:
            If (oObj.Name <> "TextBoxX") And (oObj.Name <> "TextBoxX") Then
                Set txtB(k).txtBEvent = oObj.Object: k = k + 1
            End If
        End If
       Next
       ReDim Preserve txtB(k - 1)
    End Sub
    
    1. Run the above Sub in order to allocate the event at all the text boxes on the sheet. It can be called by an event, too (or better). Use Worksheet_Activate event, for instance. Please, copy the next code in the sheet keeping the text boxes, code module:
    Option Explicit
    
    Private Sub Worksheet_Activate()
        AssignTxtBoxEvent
    End Sub
    
    1. Please, test the suggested solution and send some feedback.

    Edited:

    In order to use the suggested solution for the case of text boxes in a UserForm, please keep the same class, but assigning its event to the involved text boxes inside the UserForm_Initialize event:

    Option Explicit
    
    Private txtB() As New TxtBClass
    Private Sub UserForm_Initialize()
      Dim k As Long, oObj As Control
      ReDim txtB(100) 'maximum text boxes to be processed (it can be increased)
      
      For Each oObj In Me.Controls
        If TypeName(oObj) = "TextBox" Then
            'exclude the textboxes you need to be excluded from this common event:
            If (oObj.Name <> "TextBoxX") And (oObj.Name <> "TextBoxY") Then
                Set txtB(k).txtBEvent = oObj: k = k + 1
            End If
        End If
       Next
       ReDim Preserve txtB(k - 1)
    End Sub
    

    Please, test it and send some feedback. If this year, it will be much appreciated...