View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
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.