I guess I'm a little exhasted from testing. I cannot seem to figure something out.
I am trying to find the fastest method for finding and replacing text using VB methods.
I am using the below ReplaceX function which is a (partial)substitue for the VB Replace function.
Now, the ReplaceX function, when using binary comparision, seems to be about 75% faster!! And, with compiled code that much more again.
This is probably because the VB Replace function uses SafeArray, which calls are slow, and, there isn't the extra overhead in the ReplaceX function as there is with the Replace function, which has two additional optional parameters.
So, if you need a fast Replace function with-in VB, using binary comparison, this seems to be a good choice.
OK, the problem is when using Text comparison. The ReplaceX function seems to be 70% slower than the VB Replace function.
I guess this is due to the VB InStr function being used.
********************************************************
Now to my requests:
1. Would some of you be willing to test this and report what results you come up with?
2. Any suggestions to improve this?
3. Does anyone have a faster method for Text comparison, or at all?
Public Function ReplaceX(Expression As String, ByVal ReplaceWhat As String, ByVal ReplaceWith As String, _
Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As String
Dim lPos As Long
Dim lStrLen As Long
lPos = 1
lStrLen = Len(ReplaceWith)
Do
lPos = InStr(lPos, Expression, ReplaceWhat, CompareMethod)
If lPos > 0 Then
Expression = Left$(Expression, lPos - 1) + ReplaceWith + Mid$(Expression, lPos + Len(ReplaceWhat))
lPos = lPos + lStrLen
End If
Loop Until lPos = 0
ReplaceX = Expression
End Function
Thankyou and best regards to all! *******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!