1

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

    .......
    .......
2

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

8 Comments

@mohsin: Didn't you find some time to test the above solution? If tested, doesn't it work as you need?
First of all sorry for the delayed feedback. Your code is for the worksheet textbox. How to implement for textbox of Userform?
@mohsin: Since you were so fast in sending some feedback, I will try editing the above solution and post a variant able to work in a UserForm... :)
@mohsin: Please, test the updated solution (after Edit)...
The code is working fine. Many thanks for your help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.