View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default adding leading zero in Excel

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


"Rick Rothstein (MVP - VB)" wrote:

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