+ Reply to Thread
Results 1 to 10 of 10

Numeric code in textbox

Hybrid View

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

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

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

  4. #4
    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!

+ 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. [SOLVED] 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