View Single Post
  #24   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"GS" wrote:
Try using this optimized version I posted earlier...

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function


This function causes the following error:

Compile error:
ByRef argument type mismatch

Seems like the problem is related to the data being passed
into the "sText$" parameter, but I'm not sure.


Data type was changed from Variant in the original version to String as
last posted (shown here)...

Sub Truncate()
Const s$ = "Always eat lots of fruits and vegetables every day."
Const f$ = "and"
Dim sz$, lPos&
lPos = InStr(s, "eat")
sz = TruncateString(s, f, lPos): Debug.Print sz
End Sub

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function

...which returns "eat lots of fruits".

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion