The 10% increase comes purely from InStr vs. InStrB, LeftB$ and MidB$ are equal in speed to Left$ and Mid$
The n test is only for returning a NULL string instead of EMPTY string when there is nothing to return. A very minor detail, personally prefer returning vbNullString over "".
Edit!
Checking for Mid$ copy length does matter. If there are empty items then speed increases. Here is an updated version of DeleteBetween.
Updated attachment code includes the earlier test with some empty extra stuff. If you remove If L Then you'll see it slowing down –*testing the length is very quick.Code:Public Function DeleteBetween(ByVal sText As String, sBefore As String, sAfter As String) As String Dim P1 As Long, P2 As Long, L As Long, T As Long Dim L1 As Long: L1 = LenB(sBefore) Dim L2 As Long: L2 = LenB(sAfter) If LenB(sText) = 0 Or L1 = 0 Or L2 = 0 Then Exit Function P2 = 1 - L2 Do P2 = P2 + L2: P1 = P2 - 1 Do: P1 = InStrB(P1 + 1, sText, sBefore) Loop While (P1 And 1) = 0 And (P1 > 0) If P1 = 0 Then P1 = LenB(sText) + 1 L = P1 - P2 If L Then MidB$(sText, T + 1, L) = MidB$(sText, P2, L) T = T + L P2 = P1 + L1 - 1 Do: P2 = InStrB(P2 + 1, sText, sAfter) Loop While (P2 And 1) = 0 And (P2 > 0) Loop While P2 If T Then DeleteBetween = LeftB$(sText, T) End Function





Reply With Quote