+ Reply to Thread
Results 1 to 11 of 11

Need VBA code to add trailing zeros to numbers within text

  1. #1
    Registered User
    Join Date
    10-25-2009
    Location
    Toronto, ON
    MS-Off Ver
    Excel 2003
    Posts
    13

    Need VBA code to add trailing zeros to numbers within text

    In my table, the entire field starting at (F7 through F??) contains a text that starts with a numeric code with "-" in between the number.
    I need to scan all these records, and replace only the number part to be in the following format ##-### (that is 2 digits to the left of the "-" and 3 digits to the right of the "-", by adding trailing zeros to the left if needed. The remaining text would stay the same. All values need to be replaced in place (i.e. not copied to another field).

    Example:

    "9-50 ADJC CURD" Change to "09-050 ADJC CURD"
    "3-110 PER SAM" Change to "03-110 PER SAM"
    "11-70 REC" Change to "11-070 REC"
    "17-120 QA" remains the same as "17-120 QA"

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Need VBA code to add trailing zeros to numbers within text

    Use 00-000 instead of ##-###
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Need VBA code to add trailing zeros to numbers within text

    This formula will do the tric:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Cell A1 contains the original string.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Need VBA code to add trailing zeros to numbers within text

    Here an option with code
    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  5. #5
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Thumbs up Re: Need VBA code to add trailing zeros to numbers within text

    Got the solution...
    remember copy the final result , and paste it as string..


    Don't forget to click *
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Need VBA code to add trailing zeros to numbers within text

    Vikas, I'm not sure if the OP can use heper columns for this, "All values need to be replaced in place (i.e. not copied to another field)."

    Although, you can compress all of those helper columns into a single cell;

    =TEXT(LEFT(LEFT(B3,FIND(" ",B3)),FIND("-",LEFT(B3,FIND(" ",B3)))-1),"00")&"-"&TEXT(RIGHT(LEFT(B3,FIND(" ",B3)),LEN(LEFT(B3,FIND(" ",B3)))-FIND("-",LEFT(B3,FIND(" ",B3)))),"000")&" "&RIGHT(B3,LEN(B3)-FIND(" ",B3))

    Or shortened to
    =TEXT(LEFT(B3,FIND("-",B3)-1),"00")&TEXT(MID(B3,FIND("-",B3),FIND(" ",B3)-FIND("-",B3)),"000")&MID(B3,FIND(" ",B3),LEN(B3))
    Last edited by Speshul; 08-14-2014 at 12:26 PM.

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Need VBA code to add trailing zeros to numbers within text

    I guess I am slow on the draw. Here is my solution:

    Please Login or Register  to view this content.

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Need VBA code to add trailing zeros to numbers within text

    Sorry wrong thread.
    Last edited by Solus Rankin; 08-14-2014 at 12:33 PM. Reason: Posted to wrong thread

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

    Re: Need VBA code to add trailing zeros to numbers within text

    Try this
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    10-25-2009
    Location
    Toronto, ON
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Need VBA code to add trailing zeros to numbers within text

    Thanks All!

  11. #11
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Need VBA code to add trailing zeros to numbers within text

    Sorry If I am wrong..
    but I used helper columns for easy debugging later on..

    Vikas Gautam

+ 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] count of numbers after decimal point with trailing zeros
    By lasario in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-16-2013, 11:13 AM
  2. Replies: 5
    Last Post: 04-09-2012, 02:28 PM
  3. Numbers as text dropping trailing decimals
    By Uncle.Bungle in forum Excel General
    Replies: 5
    Last Post: 10-02-2010, 11:41 AM
  4. Keep numbers as entered with trailing zeros
    By Allie in forum Excel General
    Replies: 2
    Last Post: 08-03-2006, 01:45 PM
  5. Keep Trailing Zeros When Convert To Text
    By Todd G in forum Excel General
    Replies: 2
    Last Post: 03-23-2006, 11:20 AM

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