?
Solved

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

Posted on 2014-12-01
2
Medium Priority
?
84 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

719 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