View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tatsujin Tatsujin is offline
external usenet poster
 
Posts: 25
Default Remove leading zeros from string


?Replace("32 01 22 88 03", "0", "")


Oops! That's going to replac *ALL* zeros; - the task is to replace *LEADING*
ZEROS ONLY!!


I devised the following solution. Maybe using regular expressions
is overkill, but it worked. Here it is:

Public Sub MyReplace()
' Include"Microsoft VBScript Regular Expressions 5.5" in Tools-References
Dim regEx As New VBScript_RegExp_55.RegExp

Dim s1 As String
Dim sFinal As String
Dim sExample As String

sExample = "01 07 08 22 88 06 04"

' Replace leading zeros (in middle of line)
regEx.Pattern = " 0"
regEx.Global = True
regEx.IgnoreCase = False
s1 = regEx.Replace(sExample, " ")

' Remove leading zeros (at beginning of line)
regEx.Pattern = "^0"
regEx.Global = True
regEx.IgnoreCase = False
sFinal = regEx.Replace(s1, "")

MsgBox sFinal

End Sub


- Robert Crandall