Excel VBA to pad zeros in substring of string

I want to convert numbers to 3 decimal places for each number between the character ".".  For example:
itemno =1.1.5.2 convert itemno to = 001.001.005.002
itemno = 1.2 convert itemno to = 001.002
itemno = 4.0 convert itemno to = 004.000
itemno = 4.3 convert itemno to = 004.003
itemno = 4.10 convert itemno to = 004.010

These are item numbers in an engineering bill of material.

Is there a way to do this with a loop in excel vba?
maverick0728Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

als315Commented:
You can use this function:
Public Function cnv(A As String) As String
Dim Arr() As String, i As Integer
Arr = Split(A, ".")
cnv = ""
For i = 0 To UBound(Arr)
    If i > 0 Then cnv = cnv & "."
    cnv = cnv & Format(Arr(i), "000")
Next i
End Function

Open in new window

Sample.xlsm
1
ste5anSenior DeveloperCommented:
You need to split it and reassemble it.

Option Explicit

Public Function PadItemNumber(ANumber As String) As String

  On Local Error GoTo LocalError
  
  Dim Count As Long
  Dim Parts() As String
  
  PadItemNumber = ""
  Parts() = Split(ANumber, ".")
  For Count = 0 To UBound(Parts())
    PadItemNumber = PadItemNumber & "." & Right("000" & Parts(Count), 3)
  Next Count
  
  PadItemNumber = Mid(PadItemNumber, 2, 1024)
  Exit Function
  
LocalError:
  PadItemNumber = "#ERROR"
  
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveCommented:
A good answer from als315:

I would tend to use a very similar method apart from using Join at the end rather than concatenating in the loop:

Public Function cnv(A As String) As String
Dim Arr() As String, i As Integer

Arr = Split(A, ".")

For i = 0 To UBound(Arr)
    Arr(i) = Format(Arr(i), "000")
Next i

cnv = Join(Arr, ".")

End Function

Open in new window


This may be easier to understand and add error handling into.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.