+ Reply to Thread
Results 1 to 10 of 10

Numeric code in textbox

Hybrid View

  1. #1
    Registered User
    Join Date
    05-06-2012
    Location
    Lisbon,Portugal
    MS-Off Ver
    Excel 2010
    Posts
    79

    Numeric code in textbox

    ‘===================================================================
    Private Sub txtA_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    '’Only Numbers in textbox 0r simples signs
       Select Case KeyAscii
          Case Asc("0") To Asc("999")
          Case Asc(".")
                  If InStr(1, txtA.Value, ".") > 0 Then
                     KeyAscii = 0
                  End If
           Case Asc(",")
                   If InStr(1, txtA.Value, ",") > 0 Then
                      KeyAscii = 0
                  End If
          Case Else
                  KeyAscii = MsgBox("SORRY! Introduce just numbers")
       End Select
    End Sub
    ‘======================================================
    hello everybody
    I have a userform vba in which I have several textboxs and I intended it to be in each only registered numbering and not text.
    For a textbox individually i use the code above, but for several textboxs how to proceed ??
    Thank you for your help

  2. #2
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Numeric code in textbox

    Here is a system I use. It evaluates each key stroke as the user types it in. If the key stroke is a number then nothing happens, but if the keystroke is a letter or it will remove give an error message and remove the keystroke.

    I have created an example code for a UserFrom with two textboxes...you can incorporate into your code.

    To test create a UserForm with two textboxes named TextBox1 and TextBox2 and copy and paste the following code into it.

    Option Explicit
    Public EnableEvents As Boolean
    Public NUMvalues As String, BxStr As String, LPos As Integer, LChar As String
    Private Sub UserForm_Activate()
    NUMvalues = "0123456789."
    EnableEvents = True
    End Sub
    Private Sub TextBox1_Change() 'rename with your textbox name
    BxStr = TextBox1.Text 'rename with your textbox name
    
        If EnableEvents = True And BxStr <> "" Then
            For LPos = 1 To Len(BxStr)
                LChar = Mid(BxStr, LPos, 1)
                If InStr(NUMvalues, LChar) = 0 Then
                    MsgBox "Only numbers allowed!", vbCritical
                    RemoveBdCharacter TextBox1 'rename with your textbox name
                End If
            Next
        Else
            EnableEvents = True
            Exit Sub
        End If
    End Sub
    Private Sub TextBox2_Change() 'rename with your textbox name
    BxStr = TextBox2.Text 'rename with your textbox name
    
        If EnableEvents = True And BxStr <> "" Then
            For LPos = 1 To Len(BxStr)
                LChar = Mid(BxStr, LPos, 1)
                If InStr(NUMvalues, LChar) = 0 Then
                    MsgBox "Only numbers allowed!", vbCritical
                    RemoveBdCharacter TextBox2 'rename with your textbox name
                End If
            Next
        Else
            EnableEvents = True
            Exit Sub
        End If
    End Sub
    Private Sub RemoveBdCharacter(Bx As Variant)
        BxStr = Left(BxStr, Len(BxStr) - 1)
        EnableEvents = False
        Bx.Text = BxStr
    End Sub
    If your questions has been answered to your satisfaction please don't forget to do the following:

    Add Reputation ... and ... Mark Thread as Solved

    Thanks,

    Ma 10:8b Freely you have received; freely give.

  3. #3
    Registered User
    Join Date
    05-06-2012
    Location
    Lisbon,Portugal
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Numeric code in textbox

    Hello,Cplettner,
    Thank you for your answer.
    Nice, it works, but is it possible to join all textboxes in just one code? That´s because I have an userform with 30 textboxes, and so, the code would be very large.
    Sorry for insist, I'd be very grateful

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Numeric code in textbox

    Hi jdasp49

    You can do this by introducing a Class Module
    is it possible to join all textboxes in just one code
    See attached for an example.
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Numeric code in textbox

    Awesome @jaslake! Thanks for the tip!

  6. #6
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Numeric code in textbox

    This is the most that can be done...unfortunately you will need a separate change event for all text boxes there is no way around that to my knowledge.
    Option Explicit
    Public EnableEvents As Boolean
    Public NUMvalues As String, BxStr As String, LPos As Integer, LChar As String
    Private Sub UserForm_Activate()
    NUMvalues = "0123456789."
    EnableEvents = True
    End Sub
    Private Sub TextBox1_Change() 'rename with your textbox name
        TextBoxChange TextBox1
    End Sub
    Private Sub TextBox2_Change() 'rename with your textbox name
        TextBoxChange TextBox2
    End Sub
    Private Sub TextBoxChange(Bx As Variant)
    BxStr = Bx.Text 'rename with your textbox name
    
        If EnableEvents = True And BxStr <> "" Then
            For LPos = 1 To Len(BxStr)
                LChar = Mid(BxStr, LPos, 1)
                If InStr(NUMvalues, LChar) = 0 Then
                    MsgBox "Only numbers allowed!", vbCritical
                    RemoveBdCharacter Bx 'rename with your textbox name
                End If
            Next
        Else
            EnableEvents = True
            Exit Sub
        End If
    End Sub
    Private Sub RemoveBdCharacter(Bx As Variant)
        BxStr = Left(BxStr, Len(BxStr) - 1)
        EnableEvents = False
        Bx.Text = BxStr
    End Sub

  7. #7
    Registered User
    Join Date
    05-06-2012
    Location
    Lisbon,Portugal
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Numeric code in textbox

    Hello cplettner
    Tankyou very much, it works very well. Thank again
    Carlos

  8. #8
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Numeric code in textbox

    You are welcome!
    Please Add Reputation if you are satisfied and mark the thread as "Solved"

  9. #9
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Numeric code in textbox

    Based on a request by @YasserKahlil...to only allow one period (".") in the text box
    Change
                If InStr(NUMvalues, LChar) = 0 Then
                    MsgBox "Only numbers allowed!", vbCritical
                    RemoveBdCharacter Bx 'rename with your textbox name
                End If
    To
                If InStr(NUMvalues, LChar) = 0 Then
                    MsgBox "Only numbers allowed!", vbCritical
                    RemoveBdCharacter Bx 'rename with your textbox name
                Else
                    if Right(BxStr,1)="." and Instr(Left(Len(BxStr)-1),".")>0 then
                         MsgBox "Only one period allowed!", vbCritical
                         RemoveBdCharacter Bx
                    End if
                End If
    I think

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Numeric code in textbox

    @YasserKahlil

    Play with this in the Class Module...there's a better way but I'm using the OP's Code...

    Public WithEvents TextBoxGroup As MSForms.TextBox
    'Modified from http://www.j-walk.com/ss/excel/tips/tip44.htm
    Private Sub TextBoxGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
       '’Only Numbers in textbox 0r simples signs
       Select Case KeyAscii
       Case Asc("0") To Asc("999")
       Case Asc(".")
    '      If InStr(1, txtA.Value, ".") > 0 Then
    '         KeyAscii = 0
    '      End If
       Case Asc(",")
    '      If InStr(1, txtA.Value, ",") > 0 Then
    '         KeyAscii = 0
    '      End If
       Case Else
          KeyAscii = MsgBox("SORRY! Introduce just numbers")
       End Select
    End Sub

+ 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. Format TextBox Numeric value only
    By onmyway in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-27-2015, 03:56 AM
  2. [SOLVED] VBA code for assigning a numeric code to text; then numeric code populates table
    By cteaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 08:01 PM
  3. [SOLVED] VBA Code for Textbox: First 3 characters Alphabets next 3 characters numeric
    By honger in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-11-2013, 01:05 AM
  4. Multiplying a numeric value textbox by a duration value textbox
    By chrisjames25 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-01-2013, 05:05 AM
  5. Numeric Textbox Properties
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-09-2013, 08:31 AM
  6. Numeric Values in a textbox
    By GogsJames in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2006, 01:18 AM
  7. Format Numeric data in Textbox
    By stickandrock in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-24-2005, 09: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