+ Reply to Thread
Results 1 to 9 of 9

Adding a specified amount of zeroes in front of a number

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Adding a specified amount of zeroes in front of a number

    Hi all,

    I've found threads that discuss adding zeroes to create a fixed amount of digits but I'm interested in something different.

    First I have this:

    A1= 123
    A2= 456
    A3= 789

    Then changing the variable AddZeroes to 5 leads to:

    A1= 00000123
    A2= 00000456
    A3= 00000789


    How to do this with VBA?

    Help with this would be much appreciated

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Adding a specified amount of zeroes in front of a number

    Custom number formatting = 00000#####
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Adding a specified amount of zeroes in front of a number

    Thanks! How can I apply that in VBA?

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Adding a specified amount of zeroes in front of a number

    In the worksheet code module

    Private Sub Worksheet_Change(ByVal Target As Range)
        If IsNumeric(Target.Value) Then Target.NumberFormat = "00000#####"
    End Sub
    Last edited by AndyLitch; 01-31-2014 at 05:33 AM.

  5. #5
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Adding a specified amount of zeroes in front of a number

    I see the entire sheet is now affected. Is it possible to limit the effect to a specified column?

  6. #6
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Adding a specified amount of zeroes in front of a number

    Great, you solved it Thank you!

  7. #7
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Adding a specified amount of zeroes in front of a number

    Think I found an application for a specific column. With the below code it only works if the format of the cells is "Text". How do I get it to work if it is "General"?

    Dim i As Integer
    For i = 1 To 4
    Cells(i, 1).Value = Format(Cells(i, 1).Value, "00000####")
    Next i

  8. #8
    Registered User
    Join Date
    07-26-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Adding a specified amount of zeroes in front of a number

    The following addition seems to help

    Worksheets("Sheet1").Range("A1:A4").NumberFormat = "@"

  9. #9
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Adding a specified amount of zeroes in front of a number

    If you only want to apply it to a specific column (Say D for arguments sake) then

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column <> 4 Then Exit Sub
        If IsNumeric(Target.Value) Then Target.NumberFormat = "00000#####"
    End Sub
    or you could use :-

    If Left(Target.Address, 2) <> "$D" Then Exit Sub
    And you can restrict the rows with the Target.Row property in the same way
    Last edited by AndyLitch; 01-31-2014 at 06:25 AM.

+ 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] Adding zero (o) in front o each number in a column
    By kurtwm in forum Excel General
    Replies: 4
    Last Post: 01-03-2014, 12:56 AM
  2. Transposing list of values with 6 zeroes in front to retain the zero after transposing
    By Lisa4legin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2013, 03:34 AM
  3. [SOLVED] Adding 0's in front of string to equal a fixed number of characters
    By tdyl1969 in forum Excel General
    Replies: 3
    Last Post: 05-10-2012, 11:26 AM
  4. [SOLVED] adding a '+' in front of a positive number
    By mike in forum Excel General
    Replies: 1
    Last Post: 06-06-2005, 07:05 PM
  5. adding a zero in front of number
    By Pinky in forum Excel General
    Replies: 4
    Last Post: 04-07-2005, 05:06 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