+ Reply to Thread
Results 1 to 10 of 10

Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Question Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    I would like to have a quick single line VBA code to pad a number in string form to always have a length of a multiples of 3's.
    For example:

    "12" becomes "012"
    "1234" becomes "001234"
    "1234567" becomes "001234567"
    "12345678912345678" becomes "012345678912345678"
    and so on.

    The number string will not have any fraction or decimal points, so not to worry about that.

    The length of the resulting string should be a multiple of 3 by adding 0's to the Left.

    The number string should remain a string throughout the coding and should not be converted into a number using a numeric function such as Val, as the original string could be very long over 150 digits and using a numeric function could truncate or round the number and give undesirable results.

    I had come up with the following as a possible solution by checking the string length but feel there is a better more efficient way of doing it in one (1) coding statement.


    
    Result = String((3 - (Len(MyString) Mod 3)) Mod 3, "0") & MyString

    Thanks in advance.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,877

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    Not neccesarily any better, but another way
    String(Choose((Len(mystring) Mod 3) + 1, 0, 2, 1), "0") & mystring

  3. #3
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    Quote Originally Posted by Fluff13 View Post
    Not neccesarily any better, but another way
    String(Choose((Len(mystring) Mod 3) + 1, 0, 2, 1), "0") & mystring
    Thanks Fluff13.

    I tested it, but it is about 2.8 times slower than using a double "Mod" function.

    I was thinking my code using a double Mod function is slow.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,972

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    Formula approach:
    Formula: copy to clipboard
    =TEXT(A1,REPT(0,LEN(A1)+3-MOD(LEN(A1),3)))


    Edit: doesn't look like this works for the longer "numbers".
    Last edited by TMS; 07-03-2019 at 06:02 PM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this …


    I had the same idea as Fluff13 (aka 0, 2, 1) but via an array in order to use a single mod function :

    PHP Code: 
        Dim VMyString$, Result$
            
    = Array(021)
            
    MyString$ = "12"
            
    Result String(V(Len(MyStringMod 3), "0") & MyString 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Hi ! Try this …

    Thank you all for the ideas.

    I have come up with one more possibility with one less bracket, but still uses a double Mod function

    MyString = String(2 * (Len(MyString) Mod 3) Mod 3, "0") + MyString

  7. #7
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    After several attempts and your ideas, I have come up with the following using a single Mod Function:

    MyString = String((Len(MyString) * 2) Mod 3, "0") & MyString

    In general, a general formula would be:

    MyString = String((Len(MyString) * (Multiples-1) ) Mod Multiples, "0") & MyString
    Last edited by MohsenAlyafei; 07-04-2019 at 01:11 AM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,972

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    Bit late in the day but, as I've been playing with this, you might as well have it ...

    Option Explicit
    
    ' Formula approach: =REPT(0,3-MOD(LEN(A2),3)) & A2
    
    Sub Test1()
    With Range("A2")
        .NumberFormat = "@"
        .Value = Evaluate("=REPT(0,3-MOD(LEN(A2),3))&A2")
    End With
    End Sub
    
    Sub Test2()
    ' Loop using Evaluate
    Dim rng As Range
    Dim lLR As Long, i As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lLR
        Set rng = Range("A" & i)
        With rng
            .NumberFormat = "@"
            .Value = Evaluate("=REPT(0,3-MOD(LEN(" & rng.Address & "),3))&" & rng.Address & "")
        End With
    Next i
    End Sub
    
    Sub Test3()
    With Range("A2")
        .NumberFormat = "@"
        .Value = Application.WorksheetFunction.Rept(0, 3 - (Len(Range("A2")) Mod 3)) & Range("A2")
    End With
    End Sub
    
    Sub Test4()
    ' Loop using VBA Worksheet Functions
    Dim lLR As Long, i As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lLR
        With Range("A" & i)
            .NumberFormat = "@"
            .Value = Application.WorksheetFunction.Rept(0, 3 - (Len(Range("A" & i)) Mod 3)) & Range("A" & i)
        End With
    Next 'i
    End Sub
    Probably just overthinking it originally and trying to use TEXT/Format
    Last edited by TMS; 07-04-2019 at 09:31 AM.

  9. #9
    Registered User
    Join Date
    06-20-2019
    Location
    Qatar
    MS-Off Ver
    2019
    Posts
    33

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    Thanks TMS

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,972

    Re: Padding a Number String with Zeros to remain as a Multiple of 3's in length (in VBA)

    You're welcome. Thanks for the rep.

+ 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 value with Padding of Zeros
    By brendanbell in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-13-2018, 12:31 PM
  2. padding out zeros in textbox
    By NJH88 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-06-2013, 05:57 AM
  3. Preserve leading zeros in a varying length number when converting it to text
    By Paulymon in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-01-2013, 12:48 AM
  4. Replies: 3
    Last Post: 10-31-2012, 11:20 PM
  5. [SOLVED] Counting number of zeros between two non zero values in a string
    By simone77 in forum Excel General
    Replies: 13
    Last Post: 03-27-2012, 05:19 PM
  6. Extracting a number from a string that varies in length...
    By lucraft in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-01-2012, 11:30 AM
  7. Replies: 3
    Last Post: 05-19-2010, 04:09 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