Number to Text

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have a couple reports that Column A contains 100's of 10 digit numbers, however, some are 8 or 9 due to the leading zero(s). I am able to show the number with the leading zero, but when I import into a different database, the leading zero(s) are not recognized (and need to be).

Using a macro, how can I quickly convert 100's of cells to text so the hold the zero(s) with the leading apostrophe?
 
Hi,

It occurred to me that if some data are negative, you mightn't like the way my earlier code handles this.

So if some data are possibly negative, you may prefer the modified version
Code:
Sub keepzeros2()
Dim a
Range("A1", Cells(Rows.Count, "A").End(xlUp)).Name = "a"
With Range("a").Offset(, ActiveSheet.UsedRange.Columns.Count)
     .Cells = "=char(39) & if(a>0,char(32),char(45)) &" & _
            "rept(char(48),10-len(abs(a))) & abs(a)"
    Range("a") = .Value
    .ClearContents
End With
End Sub
This code is value-based rather than format-based, hence the leading apostrophe. Whichever type code is used should depend on the user's preferences.

But the one above allows the use of repeated formula-based data entries into columnA, whereas it seems not all format-based codes will do this without some modification.



taigovinda,

Thanks for your constructive comments. Your suggestion is included in the modified code above.

Several other workarounds available if that were a problem. One of which is making the code looping.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
mirabeau,

Thanks for the nod on that one - I wouldn't always go around mentioning the used range issue except that before someone on here showed me the fix for it, I spent months using 50+ MB files that, once fixed, were only 1MB...

Nice non-looping approach to this problem.

Tai
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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