Function ReplaceRnd(MyStr As String, Rep As String, Replacement, RepCase As Integer) As String
'Only one random replacement use Repcase = 1'
'All replacements are random use RepCase = 2'
'Change random replacement for new line use RepCase = 3'
Application.Volatile True
Dim Tmp, LenStr As Long, i As Long, X As String
Dim LenRep As Long, LenCurrent As Long
LenStr = Len(MyStr)
LenRep = Len(Rep)
LenCurrent = 0
Tmp = Split(Replacement, ";")
If RepCase = 1 Then 'Only one random replacement'
Randomize
X = Tmp(Int(Rnd() * (UBound(Tmp) + 1)))
For i = 1 To LenStr
If Mid(MyStr, i, LenRep) = Rep Then
ReplaceRnd = ReplaceRnd & X
i = i + LenRep - 1
Else
ReplaceRnd = ReplaceRnd & Mid(MyStr, i, 1)
End If
Next
Else
For i = 1 To LenStr
Select Case RepCase
Case 2 'All replacements are random'
If Mid(MyStr, i, LenRep) = Rep Then
Randomize
X = Tmp(Int(Rnd() * (UBound(Tmp) + 1)))
ReplaceRnd = ReplaceRnd & X
i = i + LenRep - 1
Else
ReplaceRnd = ReplaceRnd & Mid(MyStr, i, 1)
End If
Case 3 'Change random replacement for new line'
If i = 1 Or Mid(MyStr, i, 1) = Chr(10) Then
Randomize
X = Tmp(Int(Rnd() * (UBound(Tmp) + 1)))
End If
If Mid(MyStr, i, LenRep) = Rep Then
ReplaceRnd = ReplaceRnd & X
i = i + LenRep - 1
Else
ReplaceRnd = ReplaceRnd & Mid(MyStr, i, 1)
End If
End Select
'LenCurrent = Len(ReplaceRnd)
Next
End If
End Function