Felix18807
Programmer
I am trying to remove the spacing from strings containing initials for the purposes of record matching. To this end I created the function below.
This function creates a string for identifying possible initials within the string by converting into A's and spaces.
It is then SUPPOSED to replace initial spacing but for some reason decides to truncate my string.
It seems to fall down where there is a triple initial then a longer string.
An example of this would be
"A J T Trading" Becomes "T Trading"
but
"A W Andys" Becomes "AW Andys"
Can anyone spot where I am going wrong?
Function ReplaceText(StringIn As String) As String
Dim IDString As String
Dim TempString As String
Dim intpos As Integer
If Len(TempString) = 0 Then
ReplaceText = ""
Exit Function
End If
'Identifying Intitials
For intpos = 1 To Len(TempString)
If Mid(TempString, intpos, 1) = " " Then
IDString = IDString & " "
Else
IDString = IDString & "A"
End If
Next
Do Until InStr(IDString, "A A ") = 0 And (InStr(1, IDString, " A A")) = 0
If InStr(IDString, "A A ") > 0 Then
TempString = Replace(TempString, Chr(32), "", (InStr(1, IDString, "A A ")), 1)
IDString = Replace(IDString, "A A ", "AA ", 1, 1)
End If
If InStr(IDString, " A A") > 0 Then
TempString = Replace(TempString, Chr(32), "", (InStr(1, IDString, " A A")), 1)
IDString = Replace(IDString, " A A", " AA", 1, 1)
End If
Loop
ReplaceText = TempString
End Function
This function creates a string for identifying possible initials within the string by converting into A's and spaces.
It is then SUPPOSED to replace initial spacing but for some reason decides to truncate my string.
It seems to fall down where there is a triple initial then a longer string.
An example of this would be
"A J T Trading" Becomes "T Trading"
but
"A W Andys" Becomes "AW Andys"
Can anyone spot where I am going wrong?
Function ReplaceText(StringIn As String) As String
Dim IDString As String
Dim TempString As String
Dim intpos As Integer
If Len(TempString) = 0 Then
ReplaceText = ""
Exit Function
End If
'Identifying Intitials
For intpos = 1 To Len(TempString)
If Mid(TempString, intpos, 1) = " " Then
IDString = IDString & " "
Else
IDString = IDString & "A"
End If
Next
Do Until InStr(IDString, "A A ") = 0 And (InStr(1, IDString, " A A")) = 0
If InStr(IDString, "A A ") > 0 Then
TempString = Replace(TempString, Chr(32), "", (InStr(1, IDString, "A A ")), 1)
IDString = Replace(IDString, "A A ", "AA ", 1, 1)
End If
If InStr(IDString, " A A") > 0 Then
TempString = Replace(TempString, Chr(32), "", (InStr(1, IDString, " A A")), 1)
IDString = Replace(IDString, " A A", " AA", 1, 1)
End If
Loop
ReplaceText = TempString
End Function