• Ask a Question
Insert Image Size must be less than < 5MB.
    Ask a Question
    More answer You can create 5 answer(s).
      Ask a Poll

      is it possible to create a proc for checking for numbers only instead of keypress

      I have more than 10 textboxes that should only allow entry of numbers only.  It is very repetitive to use keypress event for every textbox … is there another way? please help


      Asked by acestar17 on January 15, 2018 in VBA: Macros.
      1 Answers

      Try this solution

      'Class Module Name "clsTextBox"
      Public WithEvents MyTextBox As MSForms.TextBox
      Private Sub MyTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      Select Case KeyAscii
      Case vbKey0 To vbKey9
      Case Else
      KeyAscii = 0
      End Select
      End Sub
      'UserForm Module
      Private arrTextBoxes() As New clsTextBox
      Private Sub UserForm_Initialize()
      Dim ctl As Control
      Dim n As Long
      For Each ctl In Me.Controls
      If TypeName(ctl) = "TextBox" Then
      n = n + 1
      ReDim Preserve arrTextBoxes(1 To n)
      Set arrTextBoxes(n).MyTextBox = ctl
      End If
      Next ctl
      End Sub
      Private Sub UserForm_Terminate()
      Erase arrTextBoxes
      End Sub
      Answered by YasserKhalil on January 25, 2018..