VBA to remove leading and trailing zeros from item numbers.

cardiaccat13

New Member
Joined
Aug 19, 2015
Messages
2
Hello, I work with a lot of item numbers that are both numeric and aphanumeric that look something like this...


00D184500
002A08800
00209100
00209200
0B3651300
07127400
07131000
0AF8230100
08310400
08310500
0H8310600
08701300
08741400
0T8741600
08741800


What I need is a formula (preferably a VBA macro I can easily just shortcut to) to remove both the leading and trailing zeros from strings of data like this. These values would be formatted as text in the data sheets I'm working with. I was able to modify a formula I found online (see below) to remove leading zeros, so I really only need the trailing ones. Would it be possible to edit this into my working macro, or do I need to create a new macro for trailing?


Here's the macro I'm using to remove the leading zeros and it's working flawlessly...


Option Explicit
Sub removezeros()

Dim myCell As Range
Dim myRng As Range
Dim iCtr As Long

With Worksheets("Sheet1")
Set myRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
For Each myCell In myRng.Cells
For iCtr = 1 To Len(myCell.Value)
If Mid(myCell.Value, iCtr, 1) <> "0" Then
Exit For
End If
Next iCtr
myCell.Value = Mid(myCell.Value, iCtr)
Next myCell
End With

End Sub



Thanks for your help!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Re: Need help with VBA to remove leading and trailing zeros from item numbers.

Welcome to the board.

How about a UDF?

Code:
Function CCTrim(sInp As String) As String
  CCTrim = Replace(Trim(Replace(sInp, "0", " ")), " ", "0")
End Function

Row\Col
A​
B​
C​
1​
00D184500D1845B1: =CCTrim(A1)
2​
002A088002A088
3​
002091002091
4​
002092002092
5​
0B3651300B36513
6​
0712740071274
7​
071310007131
8​
0AF8230100AF82301
9​
0831040083104
10​
0831050083105
11​
0H8310600H83106
12​
0870130087013
13​
0874140087414
14​
0T8741600T87416
15​
0874180087418
 
Upvote 0
Re: Need help with VBA to remove leading and trailing zeros from item numbers.

Welcome to the board.

How about a UDF?

Code:
Function CCTrim(sInp As String) As String
  CCTrim = Replace(Trim(Replace(sInp, "0", " ")), " ", "0")
End Function

Row\Col
A​
B​
C​
1​
00D184500D1845B1: =CCTrim(A1)
2​
002A088002A088
3​
002091002091
4​
002092002092
5​
0B3651300B36513
6​
0712740071274
7​
071310007131
8​
0AF8230100AF82301
9​
0831040083104
10​
0831050083105
11​
0H8310600H83106
12​
0870130087013
13​
0874140087414
14​
0T8741600T87416
15​
0874180087418

<tbody>
</tbody>

This works perfectly, thanks so much for the quick response!
 
Upvote 0
Re: Need help with VBA to remove leading and trailing zeros from item numbers.

You're welcome.
 
Upvote 0
Re: Need help with VBA to remove leading and trailing zeros from item numbers.

How about a UDF?

Code:
Function CCTrim(sInp As String) As String
  CCTrim = Replace(Trim(Replace(sInp, "0", " ")), " ", "0")
End Function

Shg, I did a search on removing trailing zeroes and this came up. Thanks. It works great and is much simpler than what I was trying. :eek:
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top