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.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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"
 
Upvote 0
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.
 
Upvote 0
In that case, maybe you'd consider using CASE statement to choose the proper number format according to the length of the code.
 
Upvote 0
Solution
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.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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