find & replace - the missing leading zero problem

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I use the following code to find and replace things like 0101.1 to 0101.A. The problem is after find & replace, what I get is 101.A, not 0101.A. The leading zero is gone. How do I preserve the leading zero? The cell is formatted as text. Thanks.


Range(Cells(6, 9), Cells(5330, 9).End(xlUp)).Select
Selection.Replace what:=".1", replacement:=".A", lookat:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How are you maintaining the leading zero in your original data? I've tested your code on cells formatted as Text and formatted as General where the value in the cell starts with an apostrophe (in order to be treated like text) and, in both case, that leading zero is retained. So I am not sure how you are set up that you are losing it.
 
Upvote 0
How are you maintaining the leading zero in your original data? I've tested your code on cells formatted as Text and formatted as General where the value in the cell starts with an apostrophe (in order to be treated like text) and, in both case, that leading zero is retained. So I am not sure how you are set up that you are losing it.
Thanks for the reply.

I formatted the cells as text by choosing "format/format cells.../text".

OK. I have to confess. Actually, I convert .A to .1 first, sort the column, then convert .1 back to .A. But I don't think sorting would have any effect on the leading zero.

The following is a real example of what I got:

051
056
070
075
80.A
81.B
 
Upvote 0
OK. I have to confess. Actually, I convert .A to .1 first

Here's the problem...
Using Find/Replace to change .A to .1 converts the value to a number (regardless of how the cell is formatted)
Leading 0's are meaningless to a number, so Excel Drops them.

What is the whole process, and what do you want to achieve?
There's likely a much better way.
 
Upvote 0
Solution
Here's the problem...
Using Find/Replace to change .A to .1 converts the value to a number (regardless of how the cell is formatted)
Leading 0's are meaningless to a number, so Excel Drops them.

What is the whole process, and what do you want to achieve?
There's likely a much better way.
If I sort with .A, it will end up at the bottom and be out of order. I want the sorted order to be something like 0305, 0401.A, 0501, not 0305, 0501, 0401.A.
 
Upvote 0
So you want it sorted by the numbers, ignoring the text parts?

Is the text on the end ALWAYS just 1 digit?

You can put a formula in another column to extract the numerical parts and sort by that column...
 
Upvote 0
OK. I have to confess. Actually, I convert .A to .1 first
That is an important "confession"... as jonmo1 has pointed out, it is where the leading zero gets removed at, not at the point in your code that you indicated in your original message.
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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