LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
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


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove leading zeros Machel Excel Discussion (Misc queries) 9 February 14th 08 12:07 AM
How can I remove leading zeros? AccessHelp Excel Programming 17 September 28th 07 07:12 PM
How can I remove leading zeros? Gary Keramidas Excel Programming 0 September 26th 07 10:03 PM
REMOVE LEADING ZEROS ichihina Excel Worksheet Functions 2 March 14th 07 07:58 PM
Using VBA to remove leading zeros Michael G. Thomas Excel Programming 5 September 12th 04 05:33 PM


All times are GMT +1. The time now is 08:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"