+ Reply to Thread
Results 1 to 7 of 7

Macro to remove Leading 0s

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2008
    Location
    sCOTLAND
    MS-Off Ver
    Office 2003 SP3
    Posts
    256

    Macro to remove Leading 0s

    I have been using the below code to remove all my leading 0s, I have now run into a little problem.

    The problem i have is once i have ran this macro to remove the leading 0s it auto formats the cells to numbers and one of my spread sheets is not picking up the numbers, is there away to adjust this code so i keep the original text format for my column?


    Sub ptest() 
    With Sheets("Sheet1").Columns("A:A")
     .NumberFormat = "0"
     .Value = .Value
     End With
     End Sub
    Last edited by JimmiOO; 10-27-2011 at 11:04 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro to remove Leading 0s

    If you want to keep the original text and have a list of numbers in the next column, it is simply a case of in column B, type = A1*1. All text "numbers" will convert to real numbers, or am I mis-reading and you are looking to do something else.

    so i keep the original text format for my column?
    Alan
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro to remove Leading 0s

    Try this. If it works then please mark the thread as "Solved"

    Sub ptest()
    Dim i As Integer
    Dim shtTest As Worksheet
    Set shtTest = ThisWorkbook.Worksheets("Sheet1")
    For i = 1 To shtTest.UsedRange.Rows.Count
        Do While VBA.Left(shtTest.Range("A" & i), 1) = "0"
            shtTest.Range("A" & i) = VBA.Mid(shtTest.Range("A" & i), 2, VBA.Len(shtTest.Range("A" & i)) - 1)
        Loop
    Next
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-07-2008
    Location
    sCOTLAND
    MS-Off Ver
    Office 2003 SP3
    Posts
    256

    Re: Macro to remove Leading 0s

    Alan i can use a different colum to extract the leading 0 but i would rather not do that if i can help it as this will be part of another fuction. All i want to do is hit a button and get rid of the leading 0s in a column keeping the original format which is text

    Kelshaer

    I have tried your option but i am getting a Run-Time error '6' Overflow, the column i am working on has over 62000 entires, that maybe the cause of this error?

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro to remove Leading 0s

    try this

    Sub ptest()
    Dim i As Long
    Dim shtTest As Worksheet
    Set shtTest = ThisWorkbook.Worksheets("Sheet1")
    For i = 1 To shtTest.UsedRange.Rows.Count
        Do While VBA.Left(shtTest.Range("A" & i), 1) = "0"
            shtTest.Range("A" & i) = VBA.Mid(shtTest.Range("A" & i), 2, VBA.Len(shtTest.Range("A" & i)) - 1)
        Loop
    Next
    End Sub

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to remove Leading 0s

    hi, JimmiOO, though I have not seen your sample workbook, try this out, run code "test"
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    07-07-2008
    Location
    sCOTLAND
    MS-Off Ver
    Office 2003 SP3
    Posts
    256

    Re: Macro to remove Leading 0s

    Thanks very much for all your help guys, Watersev not sure how you came up with that but it has done the trick, thanks alot, problem solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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