Solved

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

Posted on 2014-12-01
2
81 Views
Last Modified: 2014-12-01
Script:
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)
    Next
    On Error GoTo 0
    
    Columns("T:Z").EntireColumn.Delete
    Columns("B:R").EntireColumn.Delete
    
    
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:
00001

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.
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40473978
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.
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40473997
Hi,

or in one line

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

Regards
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question