Results 1 to 7 of 7

VBA to add leading zero.

Threaded 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
    144

    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

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. 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