+ Reply to Thread
Results 1 to 4 of 4

Restrict Userform Textbox to Numbers Only

Hybrid View

Mousiefuzz Restrict Userform Textbox to... 06-08-2015, 10:55 AM
LJMetzger Re: Restrict Userform Textbox... 06-08-2015, 01:29 PM
HaHoBe Re: Restrict Userform Textbox... 06-08-2015, 01:40 PM
Mousiefuzz Re: Restrict Userform Textbox... 06-09-2015, 04:24 AM
  1. #1
    Registered User
    Join Date
    04-29-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    33

    Restrict Userform Textbox to Numbers Only

    As the title says I've been trying to make it so that a textbox on a userform can only have numbers entered into it with an error message popping up if letters are added.

    The code I've used before doesn't seem to be working and I can't figure out why:

    Private Sub txtAccount_Change()
        OnlyNumbers
    End Sub
    
    Private Sub txtAcres_Change()
        OnlyNumbers
    End Sub
    
    Private Sub txtLivestock_Change()
        OnlyNumbers
    End Sub
    
    Private Sub txtDirectors_Change()
        OnlyNumbers
    End Sub
    
    Private Sub txtShareholders_Change()
        OnlyNumbers
    End Sub
    
    Private Sub txtPartners_Change()
        OnlyNumbers
    End Sub
    
    Private Sub OnlyNumbers()
        If TypeName(Me.ActiveControl) = "TextBox" 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
    Any ideas on a fix or what is going wrong?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Restrict Userform Textbox to Numbers Only

    Hi Mousiefuzz,

    My speculation is that you added a frame around the TextBox, and now the Frame is the Active Control.

    The following code seemed to work for me with your file:
    Private Sub OnlyNumbers()
    
        Dim sTypeName As String
        Dim sControlName As String
        Dim sValue As String
        
        'Get the TypeName of the Active Control and the Control Name
        sTypeName = TypeName(Me.ActiveControl)
        sControlName = Me.ActiveControl.Name
        
        'If the TypeName is 'Frame', get the TypeName of the Active Control
        'and the Control Name within the Frame
        If sTypeName = "Frame" Then
          sTypeName = TypeName(ActiveControl.ActiveControl)
          sControlName = ActiveControl.ActiveControl.Name
        End If
    
        'Test the Control for valid input
        If sTypeName = "TextBox" Then
            sValue = Me.Controls(sControlName).Value
            With Me.ActiveControl
                If Not IsNumeric(sValue) And sValue <> vbNullString Then
                    MsgBox "Sorry, Only numbers allowed"
                    Me.Controls(sControlName).Value = vbNullString
                End If
            End With
        End If
    End Sub
    Lewis

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Restrict Userform Textbox to Numbers Only

    Hi, Mousiefuzz,

    have a look at the KeyPress-Event behind the textbox to limit what may be entered:
    Private Sub txtYourBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      If Chr(KeyAscii) Like "[0-9]" = False Or KeyAscii = 32 Then
        Beep
        MsgBox "Only numeric values to be used!"
        KeyAscii = 0
      End If
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    04-29-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    33

    Re: Restrict Userform Textbox to Numbers Only

    Sorry for the slow reply and thank you very much for your quick answers! Unfortunately I'd left my work PC for the day when they came in but tried them out first thing this morning and both solutions work great! I honestly never knew that frames affected the code so much, guess I need to look a little more in to what they do.

    But thank you both so much for your answers, as I said, both of them worked and solved my problem perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] UserForm TextBox Validation for Whole Numbers
    By ptmuldoon in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-14-2022, 08:08 PM
  2. UserForm : textbox not filtring the numbers data
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2014, 05:55 AM
  3. Restrict textbox entries to numbers
    By domnisignis18 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-11-2013, 03:48 AM
  4. restrict textbox on userform to mm/yyyy
    By lcoreilly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2010, 11:40 AM
  5. Max numbers of characters in userform textbox and cell
    By N E Body in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1