MS Office Forum / Excel / Worksheet Functions / September 2007
adding leading zero in Excel
|
|
Thread rating:  |
ENFGDC4S - 14 Sep 2007 18:16 GMT How do I add zeros to numbers in a cell? ex: R1,R12,R152 to R001,R012,R152 This needs to be done in many cells. Thanks
Gary''s Student - 14 Sep 2007 18:48 GMT =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000")
 Signature Gary''s Student - gsnu200744
ENFGDC4S - 14 Sep 2007 19:26 GMT I could not get this to work.
> =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") Gary''s Student - 14 Sep 2007 19:32 GMT That's O.K. We can solve this.
In A1 thru A3 I entered:
R1 R12 R152
I put my formula in B1 thru B3 and see:
R1 R001 R12 R012 R152 R152
What do you see?
 Signature Gary''s Student - gsnu200744
> I could not get this to work. > > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") ENFGDC4S - 14 Sep 2007 19:42 GMT This is where my problem is. Here is an example of one of the cells: C5,C16,C30,C38,C54,C91,C98,C136,C147 I can have a few hundred of these alpha numeric characters in one cell. Thanks for your time.
> That's O.K. We can solve this. > [quoted text clipped - 15 lines] > > > > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") Gary''s Student - 14 Sep 2007 20:44 GMT O.K.
You see my mis-understanding.
I can solve this with visual basic.
Check back later
 Signature Gary''s Student - gsnu200744
> This is where my problem is. Here is an example of one of the cells: > C5,C16,C30,C38,C54,C91,C98,C136,C147 [quoted text clipped - 20 lines] > > > > > > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") Rick Rothstein (MVP - VB) - 14 Sep 2007 21:47 GMT > I can solve this with visual basic. I'll save you the trouble.<g>
If there is only one leading letter character for each "field" in the cell and if the number part always needs to be three digits long, then this sub should work...
Sub AddZeroes() Dim C As Range Dim Fields() As String For Each C In Selection Fields = Split(C.Value, ",") For X = 0 To UBound(Fields) Fields(X) = Left$(Fields(X), 1) & _ Format$(Mid$(Fields(X), 2), "000") Next C.Value = Join(Fields, ",") Next End Sub
To ENFGDC4S: ============= All you have to do is right-click the sheet tab at the bottom, and "View Code"; then copy/paste the above routine into the sheet's code window. Once you have done that, go back to the spreadsheet and select the cells you want to apply this routine to and then press Alt+F8 and run the AddZeroes macro from the dialog box that appears.
Rick
Gary''s Student - 14 Sep 2007 23:28 GMT Very nice
Thank you for introducing me to JOIN()
 Signature Gary''s Student - gsnu200744
> > I can solve this with visual basic. > [quoted text clipped - 26 lines] > > Rick Rick Rothstein (MVP - VB) - 15 Sep 2007 01:20 GMT > Thank you for introducing me to JOIN() You are quite welcome. As the companion function to Split, it is really a very nice function to have in the arsenal.
Rick
>> > I can solve this with visual basic. >> [quoted text clipped - 31 lines] >> >> Rick Rick Rothstein (MVP - VB) - 14 Sep 2007 21:29 GMT I guess we should further clarify... is the part in front of the number always a SINGLE letter character? Is the number part always going to be three digits long? Any other info we should know?
Rick
> This is where my problem is. Here is an example of one of the cells: > C5,C16,C30,C38,C54,C91,C98,C136,C147 [quoted text clipped - 20 lines] >> > >> > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") ENFGDC4S - 14 Sep 2007 22:02 GMT The alpha character will be a single character and followed by up to 3 numerals. There is a posibility of up to 4 numerals but does not happen much. 3 numerals will be fine I would only need the leading zeros if the numeral is from 1 - 99, for example, C3,C34 would be C003,C034. There is also a comma after each item. These are Reference Designators on a printed wiring board and are used in the parts list for idenification.
> I guess we should further clarify... is the part in front of the number > always a SINGLE letter character? Is the number part always going to be [quoted text clipped - 26 lines] > >> > > >> > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") Rick Rothstein (MVP - VB) - 14 Sep 2007 22:09 GMT Okay, see my response to Gary''s Student where the code I posted anticipated the answer you gave.
Rick
> The alpha character will be a single character and followed by up to 3 > numerals. [quoted text clipped - 37 lines] >> >> > >> >> > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000") ENFGDC4S - 17 Sep 2007 14:30 GMT The sub worked great! Thank you for your time and effort. Eric
> Okay, see my response to Gary''s Student where the code I posted anticipated > the answer you gave. [quoted text clipped - 42 lines] > >> >> > > >> >> > > =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"000")
|
|
|