VBA Replace Method is Deleting Leading Zeros

Matthew Tapp

New Member
Joined
Nov 30, 2020
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
I have built a VBA macro to do a find and replace process on a specified range, referencing ranges for the "what" and "replacement" terms. The find and replace works but it is removing leading zeros from the replacement terms.

Here is a simplified sample of the code....
Range("A1").Replace What:=Range("B1"), Replacement:=Range("C1"), LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=True,

Cell C1 refers to a 3-digit code that begins with a zero (ex. 020). (Please note: this is a simple version of the code I've been using to trial and error this issue. The full code references a table and loops through a series of numbers, some beginning with zero and some not.). The number format of C1 is set to text. I've also added an apostrophe beforehand (i.e. '020).

When I run this macro, it removes the leading zero and writes "20" in A1. I've tried numerous things to correct this, with no success, including defining the ReplaceFormat in the code, defining the number format of A1, and using the substitute method instead of replace.

The code works if I the replacement value is spelled out in the code (i.e. "'020") rather than referencing a range. It only fails to work when referencing a range. I don't want to explicitly write out the replacement terms because there's dozens and the list can grow, so I want users who don't know VBA to be able to add to the list as needed).

Additionally, I noticed a peculiar quirk... if the target range (A1) previously contained the value 020, then it will write it correctly, even after formatting is deleted.
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,832
Office Version
  1. 2010
Platform
  1. Windows
Is A1 also set to text?
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,832
Office Version
  1. 2010
Platform
  1. Windows
I added the following line before the replace code. It works but it may not be a general solution.

VBA Code:
Range("A1").NumberFormat = "000"
 

Matthew Tapp

New Member
Joined
Nov 30, 2020
Messages
4
Office Version
  1. 365
Platform
  1. MacOS

ADVERTISEMENT

I added the following line before the replace code. It works but it may not be a general solution.

VBA Code:
Range("A1").NumberFormat = "000"
Thanks, I thought about doing this. It will work for now since all our current codes are three digits, but technically these codes could be 1-4 digits in length.
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,832
Office Version
  1. 2010
Platform
  1. Windows
In that case, maybe you'd consider using CASE statement to choose the proper number format according to the length of the code.
 
Solution

Matthew Tapp

New Member
Joined
Nov 30, 2020
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
In that case, maybe you'd consider using CASE statement to choose the proper number format according to the length of the code.
This worked well! I wasn't familiar with CASE, but looked it up and it was easy to implement. Thanks for your help! I wish I understood why the Replace method was getting rid of the zeros, but this is a solid workaround.
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,832
Office Version
  1. 2010
Platform
  1. Windows
Glad your problem is solved.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,998
Messages
5,622,097
Members
415,876
Latest member
csibonga2k17

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
Top