+ Reply to Thread
Results 1 to 7 of 7

convert a number into 3 digits.

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    convert a number into 3 digits.

    Hi guys

    The user enters a number in textbox1,I want to convert in into 3 digits and store it in a variable. Suppose textbox1.value=5 then the variable a =005
    If textbox1.value=10 then a=010

    I hope canyone can help me out.

    Thanks
    Aman

  2. #2
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: convert a number into 3 digits.

    A would have to be a string to save it as 3 digits with leading zeros

    A = right("000" & textbox1.text,3)
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


  3. #3
    Registered User
    Join Date
    05-28-2010
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: convert a number into 3 digits.

    You can try something like this :

    Sub digit()
    Dim a As Variant
    a = InputBox("3 digits")
    If Len(a) = 1 Then
    a = "00" & a
    MsgBox a
    Else
    End If
    End Sub

  4. #4
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: convert a number into 3 digits.

    Quote Originally Posted by le6tata View Post
    You can try something like this :

    Sub digit()
    Dim a As Variant
    a = InputBox("3 digits")
    If Len(a) = 1 Then
    a = "00" & a
    MsgBox a
    Else
    End If
    End Sub
    Just to point out, variants should be avoided where possible as its bad programming, since we know that A is a string Dim A as String is correct!

    and also the else is unnesessary, besides the faxt that if i input 10 I get 10 out rather than 010!

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: convert a number into 3 digits.

    Try this in your UserForm Code

    Please Login or Register  to view this content.

    The user can only enter numbers and is limited to a total of three.

    I hope this might be of some help.
    Last edited by Marcol; 05-28-2010 at 06:10 AM.

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: convert a number into 3 digits.

    @ le6tata,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: convert a number into 3 digits.

    The simplest method: (use what is available in VBA)

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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