Solved

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

Posted on 2014-12-01
2
79 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 51

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 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