+ Reply to Thread
Results 1 to 7 of 7

VBA to add leading zero.

Hybrid View

mowens74 VBA to add leading zero. 05-12-2020, 12:20 PM
Logit Re: VBA to add leading zero. 05-12-2020, 12:34 PM
xladept Re: VBA to add leading zero. 05-12-2020, 12:42 PM
Rick Rothstein Re: VBA to add leading zero. 05-12-2020, 06:59 PM
Rick Rothstein Re: VBA to add leading zero. 05-12-2020, 07:14 PM
mowens74 Re: VBA to add leading zero. 05-13-2020, 07:25 AM
xladept Re: VBA to add leading zero. 05-13-2020, 11:01 AM
  1. #1
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2016
    Posts
    142

    VBA to add leading zero.

    I am trying to run VBA code to add a leading zero so that the figures are 2 in length;
    e.g. 1 becomes 01

    I have looked around and found some code, it doesn't error but it doesn't add a leading zero when run.
    Sub Leading_Zero()
    Dim LZero As String
    Application.ScreenUpdating = False
    
    LZero = Empty
    
    Worksheets(1).Columns("A").NumberFormat = "@"
    Range("A1").Select
    If Len(Cell) < 2 Then
            Cell = WorksheetFunction.Rept("0", 1 - Len(Cell)) & Cell
    End If
    
    Do Until LZero = "End"
        ActiveCell.Offset(7, 0).Activate
        If ActiveCell.Value <> "" Then
            If Len(Cell) < 2 Then
                Cell = WorksheetFunction.Rept("0", 1 - Len(Cell)) & Cell
            End If
        ElseIf ActiveCell.Value = "" Then
            LZero = "End"
        End If
    Loop
    
    Application.ScreenUpdating = True
    Range("A1").Select
    End Sub
    I have attached an example sheet.
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,438

    Re: VBA to add leading zero.

    https://excelchamps.com/blog/leading-zeros/

    Look at #5

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to add leading zero.

    Or try this:

    Sub Vulnerability_Review_Sort()
    Dim LZero As String: LZero = Empty
    'Application.ScreenUpdating = False
    Worksheets(1).Columns("A").NumberFormat = "@"
    Range("A1").Select
                        Do
    If Len(ActiveCell) < 2 Then ActiveCell = "0" & ActiveCell
            ActiveCell.Offset(7).Activate
            Loop Until ActiveCell = LZero
    Range("A1").Select: Application.ScreenUpdating = True: End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBA to add leading zero.

    Here is another macro (no loops) that should also work...
    Sub Vulnerability_Review_Sort()
      With Range("A1", Cells(Rows.Count, "A").End(xlUp))
        .SpecialCells(xlConstants).NumberFormat = "@"
        .Value = Evaluate(Replace("IF(LEN(@),TEXT(@,""00""),"""")", "@", .Address))
      End With
    End Sub

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBA to add leading zero.

    Actually, in thinking about it some more... given the OP's layout, we only need to examine up to Row 57 (that is, the cells containing either blanks or the first 9 numbers) since after that, all number cells will consist of two digits (assuming the OP's numbering stops at 99 maximum). Given that, this macro should also work...
    Sub Vulnerability_Review_Sort()
      [A1:A57].NumberFormat = "@"
      [A1:A57] = [IF(LEN(A1:A57),TEXT(A1:A57,"00"),"")]
    End Sub

  6. #6
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2016
    Posts
    142

    Re: VBA to add leading zero.

    Thank you all for your help, I have gone with Rick's last submission with a little edit :-D
    Worksheets(1).Columns("A").NumberFormat = "@"
    [A1:A57] = [IF(LEN(A1:A57),TEXT(A1:A57,"00"),"")]

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to add leading zero.

    You're welcome and 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. Help with leading 0's
    By pleasir in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-07-2017, 07:18 PM
  2. Leading Zero's
    By HangMan in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-10-2013, 10:21 PM
  3. leading Zero
    By mma3824 in forum Excel General
    Replies: 2
    Last Post: 10-31-2012, 11:45 AM
  4. Leading zero
    By rofl in forum Excel General
    Replies: 4
    Last Post: 01-24-2010, 04:07 PM
  5. Leading 0's
    By exsam21 in forum Excel General
    Replies: 5
    Last Post: 01-20-2006, 02:40 PM
  6. leading 0
    By cb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-13-2005, 10:06 AM
  7. [SOLVED] Add a leading zero
    By yahoo in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 03-12-2005, 03:06 PM

Tags for this Thread

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