Function Repc(ByVal s As String) As String
Dim o, r, n As Long
s = " " & s & " "
o = Array(" Abda ", " Alam ")
r = Array(" Abdallah ", " Allam ")
For n = LBound(o) To UBound(o)
Repc = Replace(s, o(n), r(n)): s = Repc
Next n
Repc = Application.Trim(s)
End Function
This is because you have two occurrences of Abda next to each other with only one space in between. If there had been two spaces, it would have worked.
Function Repc(ByVal s As String) As String
Dim o, r, n As Long
s = " " & s & " "
o = Array(" Abda ", " Alam ")
r = Array(" Abdallah ", " Allam ")
For n = LBound(o) To UBound(o)
Do While InStr(s, o(n))
s = Replace(s, o(n), r(n))
Loop
Next n
Repc = Application.Trim(s)
End Function
Function Repc(ByVal s As String) As String
Dim o, r, n As Long
s = " " & Replace(s, " ", " ") & " "
o = Array(" Abda ", " Alam ")
r = Array(" Abdallah ", " Allam ")
For n = LBound(o) To UBound(o)
s = Replace(s, o(n), r(n))
Next n
Repc = Application.Trim(s)
End Function
Thanks a lot my tutor. I have tested both and I found the second workaround is more suitable and faster than the first one.
Thank you very much.
But I am still didn't get the reason .. The problem was with the third ccurrence not with the second one
This was the result
Abdallah Allam Abdallah Abda
and if the problem with s[ace in the replacement array, I put space before and after, so what's the problem exactly or how did it work?
The replace works from left to right. After replacing the second " Abda " with "Abdallah ", it resumes looking at the remaining part of the string after " Abdallah ". That is only "Abda ". There is no " Abda " anymore.
Forgive me. If it works from left to right then
" Abda Alam Abda Abda " >> this is my original string
for the first step it would be " Abdallah Alam Abda Abda "
for the next step it would be " Abdallah Alam Abdallah Abda "
Here is the problem in the last occurrence it is supposed " Abda " is there in the string so I epxect to be replaced to be like that " Abdallah Alam Abdallah Abdallah "