Converting Numbers to Text to Keep Leading 0's - Dynamic Range

ac1212

New Member
Joined
Jul 13, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I am trying to covert numbers in in a column to text in order to keep the leading zero's. I have managed to get the below to work but want to be able to have the range as dynamic not set on the end being row 100.

I need to have all cells with numbers in column A to be 5 characters long with leading zero's if needed.

Thanks for the help!

VBA Code:
  Dim cell As Range
   
    Range("A2:A100").NumberFormat = "@"
   
    For Each cell In Range("A2:A100")
        cell = Format(cell, "00000")
    Next cell
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
ac1212 welcome to the forum.

You don't need a VBA solution for your case. You can simply select the column or range and press Ctrl + 1 and use 00000 custom format
 
Upvote 0
ac1212 welcome to the forum.

You don't need a VBA solution for your case. You can simply select the column or range and press Ctrl + 1 and use 00000 custom format
Thanks for the response Sanjeev. I should have clarified that this is to format numerous columns from an accounting database. There are other formatting issues that I have already resolved, just stuck on this one.
 
Upvote 0
You need to be a little bit more clear as to what you want here. Formatting doesnt change the value of a cell. It changes its appearance. So when you say you want the cells to be all 5 characters long then formatting isnt going to make them longer but they will just appear longer.

For example lets say your cell contains the number 1. Changing the format can make that appear as 00001 but the underlying value is still 1 and =LEN(A1) would still produce 1 not 5.

So do you want the cell value to actually be 1 or 00001? There is a difference.
 
Upvote 0
You need to be a little bit more clear as to what you want here. Formatting doesnt change the value of a cell. It changes its appearance. So when you say you want the cells to be all 5 characters long then formatting isnt going to make them longer but they will just appear longer.

For example lets say your cell contains the number 1. Changing the format can make that appear as 00001 but the underlying value is still 1 and =LEN(A1) would still produce 1 not 5.

So do you want the cell value to actually be 1 or 00001? There is a difference.

I want the cell value to be 00001.

Essentially the code I originally posted does the job, however its only for the range A2:A100. I need this to be on dynamic, the range in column A could be 2 rows or thousands depending on the data so want this to be done for all cells with data in column A.
 
Upvote 0
For exactly the reasons i prescribed your original code doesnt do the job. You are just formatting. You are NOT altering the cell value at all. You are changing the appearance.
 
Upvote 0
If you just want formatting then why not just format the entire column:

VBA Code:
Columns("A:A").NumberFormat = "00000"

You never need to loop to do that.
 
Upvote 0
For example currently there is “1201” in a cell in column A, I need this to be changed to “01201”. The original code I posted does this. However the data is not always the same number of rows, if I change the range from “A2:A100” to “A:A” it causes excel not to respond. I assume because it is trying to change all rows in column A, that’s why I want to change it to a dynamic range that only changes the cells in column A that contain data.
 
Upvote 0
How about
VBA Code:
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .NumberFormat = "@"
      .Value = Evaluate(Replace("if(@="""","""",text(@,""00000""))", "@", .Address))
   End With
 
Upvote 0
Solution

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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