Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Removing Leading Zeros from a String

Indeed, will change that and see what the
difference is.

RBS


<GatesAntiChrist wrote in message
. ..
All replies so far have moved to Mid(...i...) instead of
Right(...Len(strNumber)...) so this could now be moot in the final
objective ... but for Smissaert - in the spirit of "fastest possible
function" - I would think about hoisting the Len(). My understanding
of VBA's interpretative nature suggests that it would recalculate it on
every iteration. Ouch!

And at the risk of sounding completely insane, is Byte actually faster
than Integer? Or even Long?!

Sincerely,
An obsolete .ASM dinosaur

Your code wont work on non unicode systems (due to step2)

A small change in your code gives 5-10% improvement:
Change: StripLeadingZeros = Right(strNumber, Len(strNumber) - i / 2)
TO : StripLeadingZeros = Mid(strNumber, i / 2)


With an average of less than 4 leading zero's following code is faster:

Function TrimLeadZero$(ByVal strNumber$)
While strNumber Like "0*"
strNumber = Mid(strNumber, 2)
Wend
TrimLeadZero = strNumber
End Function

(I admit it is marginal, and advantage lost with longer strings)

<bg


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"RB Smissaert" wrote:

If you have a large number of cells to strip you may want the fastest
possible
function. I think this is quite efficient:

Function StripLeadingZeros(strNumber As String) As String

Dim i As Long
Dim byteArray() As Byte

byteArray = strNumber

For i = 0 To UBound(byteArray) Step 2
If Not byteArray(i) = 48 Then
StripLeadingZeros = Right(strNumber, Len(strNumber) - i /

2)
Exit For
End If
Next

End Function

If somebody can show me something that is faster I would be interested.


Reply
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
Stop automatically removing leading zeros? Jessica P[_2_] Excel Discussion (Misc queries) 2 November 17th 08 09:47 PM
How do you stop excel removing the leading zeros in a cell? ck2007 Excel Discussion (Misc queries) 2 May 25th 07 06:16 PM
Removing leading zeros from a group of numbers MAC253 Excel Discussion (Misc queries) 3 June 14th 06 09:49 PM
removing leading zeros in numeric fields dingy101 Excel Discussion (Misc queries) 3 November 21st 05 03:46 AM
save text field w/ leading zeros in .csv format & not lose zeros? Ques Excel Discussion (Misc queries) 1 May 4th 05 06:21 PM


All times are GMT +1. The time now is 10:09 PM.

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"