1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Public Function Replace( _
ByVal expression As String, _
ByVal find As String, _
ByVal replacewith As String, _
Optional ByVal start As Long = 1, _
Optional ByVal count As Long = -1, _
Optional ByVal compare As Long)
Dim lenFind As Long
Dim lenNext As Long
Dim lenReplace As Long
Dim lenExpression As Long
'Emulation de Const vbUseCompareOption = -1
If IsEmpty(compare) Or _
((compare <> vbBinaryCompare) And (compare <> vbTextCompare)) Then
compare = IIf("A" = "a", vbTextCompare, vbBinaryCompare)
End If
If start > Len(expression) Then
Replace = vbNullString
Else
If Not ((start < 1) Or _
(start > Len(expression)) Or (find = vbNullString)) Then
lenFind = Len(find)
lenReplace = Len(replacewith)
start = InStr(start, expression, find, compare)
Do While (start <> 0) And (count <> 0)
lenExpression = Len(expression)
lenNext = start + lenFind
expression = Mid$(expression, 1, start - 1) & replacewith & _
IIf(lenNext <= lenExpression, _
Mid$(expression, lenNext), vbNullString)
count = count - 1
start = InStr(start + lenReplace, expression, find, compare)
Loop
End If
Replace = expression
End If
End Function |
Partager