+ Reply to Thread
Results 1 to 9 of 9

Ensuring textbox has specified number of digits

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Ensuring textbox has specified number of digits

    Hi all

    I was just wondering how I would go about ensuring that a textbox could auto format to display the correct number of digits that I need, in this case 6, i.e.


    If the user was to type "1" the textbox would display "000001" and if the the user was to type "1486" the textbox wouyld display "001486" and so on...

    I'm assuming that this would fall within an AfterUpdate sub?

    Many thanks,

    Andy

  2. #2
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Ensuring textbox has specified number of digits

    Perhab like this
    Or you can do using textbox_upterUpdate
    private sub textbox1_change()
    Dim t$,t1$
    If isnumeric(textbox1.value) = true then 
      t = textbox1.value
      t1 = 000000
      If len(t1) > len(t) then
        Textbox1.value = StrReverse(Replace(strReverse(t1),0,t,1,len(t)))
      End if
    End if
    End sub
    Last edited by daboho; 11-07-2018 at 06:48 PM.

  3. #3
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Ensuring textbox has specified number of digits

    Thanks Daboho

    I've just tried that but t=000000 keeps changing to t=0 when I enter it.

  4. #4
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513
    Quote Originally Posted by AndyE View Post
    Thanks Daboho

    I've just tried that but t=000000 keeps changing to t=0 when I enter it.
    Try Change to event upter update
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Ensuring textbox has specified number of digits

    Or try this not test
    const t1 as string =000000
    private sub textbox1_AfterUpdate()
    If isnumeric(textbox1.value) = true then 
      t = textbox1.value
      If len(t1) > len(t) then
        Textbox1.value = StrReverse(Replace(strReverse(t1),0,t,1,len(t)))
      End if
    End if
    End sub

  6. #6
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Ensuring textbox has specified number of digits

    t1 will not hold the 000000 value. It keeps reverting to 0.

  7. #7
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Ensuring textbox has specified number of digits

    Private Sub TextBox1_Change()
    Dim t1$
    t1 = TextBox1.Value
    t1 = String(6 - Len(t1), "0") & t1
    MsgBox t1
    TextBox1.Value = t1: t1 = ""
    End Sub

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Ensuring textbox has specified number of digits

    AndyE,

    Try this
    Private Sub TextBox1_Change()
        If Len(Me.TextBox1.Value) = 0 Then Exit Sub
        With Me.TextBox1
            .Value = Format$(Val(.Value), "000000")
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Ensuring textbox has specified number of digits

    Thank you.

+ 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] Limit number entry in Userform TextBox to exactly 6 digits
    By Twaddy006 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2014, 10:53 AM
  2. [SOLVED] VBA to convert cell to text and ensuring 7 digit number all along in entire column.
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-19-2013, 09:35 AM
  3. [SOLVED] Wrong calculation results in userform textbox when using more than 3 digits for a number
    By Shacker in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 05-06-2013, 03:33 PM
  4. Replies: 5
    Last Post: 12-08-2008, 11:47 AM
  5. Ensuring one number is a whole number greater than the other
    By key in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-15-2008, 01:39 PM
  6. [SOLVED] Digits in textbox's
    By Greg B in forum Excel General
    Replies: 4
    Last Post: 09-04-2005, 02:05 PM
  7. Digits in textbox's
    By Greg B in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-04-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