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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][/tr][tr][td]
1​
[/td][td]00D184500[/td][td]D1845[/td][td]B1: =CCTrim(A1)[/td][/tr]
[tr][td]
2​
[/td][td]002A08800[/td][td]2A088[/td][td][/td][/tr]
[tr][td]
3​
[/td][td]00209100[/td][td]2091[/td][td][/td][/tr]
[tr][td]
4​
[/td][td]00209200[/td][td]2092[/td][td][/td][/tr]
[tr][td]
5​
[/td][td]0B3651300[/td][td]B36513[/td][td][/td][/tr]
[tr][td]
6​
[/td][td]07127400[/td][td]71274[/td][td][/td][/tr]
[tr][td]
7​
[/td][td]07131000[/td][td]7131[/td][td][/td][/tr]
[tr][td]
8​
[/td][td]0AF8230100[/td][td]AF82301[/td][td][/td][/tr]
[tr][td]
9​
[/td][td]08310400[/td][td]83104[/td][td][/td][/tr]
[tr][td]
10​
[/td][td]08310500[/td][td]83105[/td][td][/td][/tr]
[tr][td]
11​
[/td][td]0H8310600[/td][td]H83106[/td][td][/td][/tr]
[tr][td]
12​
[/td][td]08701300[/td][td]87013[/td][td][/td][/tr]
[tr][td]
13​
[/td][td]08741400[/td][td]87414[/td][td][/td][/tr]
[tr][td]
14​
[/td][td]0T8741600[/td][td]T87416[/td][td][/td][/tr]
[tr][td]
15​
[/td][td]08741800[/td][td]87418[/td][td][/td][/tr]
[/table]
 
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

[TABLE="class: grid"]
<tbody>[TR]
[TD]Row\Col[/TD]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[TD]
C​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD]00D184500[/TD]
[TD]D1845[/TD]
[TD]B1: =CCTrim(A1)[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]002A08800[/TD]
[TD]2A088[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]00209100[/TD]
[TD]2091[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]00209200[/TD]
[TD]2092[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]0B3651300[/TD]
[TD]B36513[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]07127400[/TD]
[TD]71274[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]07131000[/TD]
[TD]7131[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]0AF8230100[/TD]
[TD]AF82301[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
9​
[/TD]
[TD]08310400[/TD]
[TD]83104[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
10​
[/TD]
[TD]08310500[/TD]
[TD]83105[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
11​
[/TD]
[TD]0H8310600[/TD]
[TD]H83106[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
12​
[/TD]
[TD]08701300[/TD]
[TD]87013[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
13​
[/TD]
[TD]08741400[/TD]
[TD]87414[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
14​
[/TD]
[TD]0T8741600[/TD]
[TD]T87416[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
15​
[/TD]
[TD]08741800[/TD]
[TD]87418[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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. :eeek:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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