How can I modify this script to be used in an Excel macro?

Option Explicit
Sub Clean_Up()
    Dim rng As Range
    Dim rng2 As Range
    Dim cl As Object
    Dim lngRow As Long
    Set rng = Range("A1:A" & Cells.SpecialCells(xlLastCell).Row + 1)
    On Error Resume Next
    For lngRow = 1 To rng.Rows.Count
    ActiveSheet.Cells(lngRow, 1).Value = Split(ActiveSheet.Cells(lngRow, 1).Value, " ")(0)
    On Error GoTo 0
End Sub

Open in new window

I would like to modify the script so that when it find numbers in column A, that it does not remove the leading 0's to those numbers but leaves them intact, and still goes on with the function set out for the macro.

For instance if it finds a line in column A which has the following:
00001  BULK DESCRIPTION 20 lbs

It will simply leave it as 1.

I want it to be:

It seems to work fine if there are alpha characters, but if it's numbers, then it removes leading zeros and it should not.

Can we modify the script if possible?

Any assistance is appreciated.

Thank you.
Who is Participating?
Rgonzo1971Connect With a Mentor Commented:

or in one line

ActiveSheet.Cells(lngRow, 1).Formula = "'" & Split(ActiveSheet.Cells(lngRow, 1).Value, " ")(0)

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Insert an extra line before row 12:

 ActiveSheet.Cells(lngRow, 1).NumberFormat = "@"

Open in new window

That converts that cell to text, and so your 00001 remains 00001.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.