Why write when you can Copy-n-Paste? ;)
From [link|http://www.tek-tips.com/gfaqs.cfm/pid/705/fid/4342|http://www.tek-tips..../pid/705/fid/4342]
\n'------------------------------------------------------------------------------------\nPublic Function Replace(Expression As String, _\n Find As String, ByVal ReplaceWith As String, _\n Optional Start As Long = 1, Optional Count As Long = -1, _\n Optional Compare As Integer = vbBinaryCompare) As String\n'------------------------------------------------------------------------------------\n' Purpose: Replaces occurrences of a substring within a string with a specified\n' repacement string.\n' Accepts: 1. String containing substrings to be replaced\n' 2. Value of the substring to be found\n' 3. Replacement value to substitute for the substring\n' 4. Optional starting position for the search, default=1 (start of string)\n' 5. Optional maximum number of occurrences to replace, default=-1 (all\n' occurences)\n' 6. Optional text comparison method (default: binary compare)\n' Returns: The part of the string starting at the Start position, with substitutions\n' made. Returns an empty string if Start > Len(Expression).\nDim str As String\nDim i As Long\nDim lngFindLen As Long\nDim c As Long\n\nIf Start < 1 Then Err.Raise 5\nIf Count < -1 Then Err.Raise 5\nIf Compare < 0 Or Compare >= 40 Then Err.Raise 5\nstr = Mid$(Expression, Start)\nlngFindLen = Len(Find)\nIf lngFindLen > 0 Then\nc = Count\ni = 1\nDo While c <> 0\ni = InStr(i, str, Find, Compare)\nIf i = 0 Then Exit Do\nIf lngFindLen = Len(ReplaceWith) Then\nMid$(str, i, lngFindLen) = ReplaceWith\nElse\nstr = Left$(str, i - 1) & ReplaceWith & Mid$(str, i + lngFindLen)\nEnd If\ni = i + Len(ReplaceWith)\nIf c > 0 Then c = c - 1\nLoop\nEnd If\nReplace = str\nEnd Function\n
Edited by
altmann
Jan. 17, 2004, 01:46:47 AM EST
Why write when you can Copy-n-Paste? ;)
From [link|http://www.tek-tips.com/gfaqs.cfm/pid/705/fid/4342|http://www.tek-tips..../pid/705/fid/4342]
'------------------------------------------------------------------------------------
' Purpose: Replaces occurrences of a substring within a string with a specified
' repacement string.
' Accepts: 1. String containing substrings to be replaced
' 2. Value of the substring to be found
' 3. Replacement value to substitute for the substring
' 4. Optional starting position for the search, default=1 (start of string)
' 5. Optional maximum number of occurrences to replace, default=-1 (all
' occurences)
' 6. Optional text comparison method (default: binary compare)
' Returns: The part of the string starting at the Start position, with substitutions
' made. Returns an empty string if Start > Len(Expression).
Dim str As String
Dim i As Long
Dim lngFindLen As Long
Dim c As Long
If Start < 1 Then Err.Raise 5
If Count < -1 Then Err.Raise 5
If Compare < 0 Or Compare >= 40 Then Err.Raise 5
str = Mid$(Expression, Start)
lngFindLen = Len(Find)
If lngFindLen > 0 Then
c = Count
i = 1
Do While c <> 0
i = InStr(i, str, Find, Compare)
If i = 0 Then Exit Do
If lngFindLen = Len(ReplaceWith) Then
Mid$(str, i, lngFindLen) = ReplaceWith
Else
str = Left$(str, i - 1) & ReplaceWith & Mid$(str, i + lngFindLen)
End If
i = i + Len(ReplaceWith)
If c > 0 Then c = c - 1
Loop
End If
Replace = str
End Function
--
Chris Altmann