I have this macro:
Sub StringChecker()
Dim string_arr() As Variant
Dim k As Integer
Dim c As Range
Set c = ActiveSheet.[A1]
end_string = Array(" &", _
" TR", _
" SR", _
" DEFEN")
substring = Array(" SR ", _
" JR ")
Do While c <> "End Loop"
c.Offset(0, 1) = c
For k = 0 To UBound(end_string)
If Right(c, Len(end_string(k))) = end_string(k) Then
cleaner_string = Mid(c, 1, Len(c) - Len(end_string(k)))
End If
Next k
clean_string = cleaner_string
For l = 0 To UBound(substring)
clean_string = Replace(clean_string, substring(l), " ")
Next l
If clean_string = "" Then
clean_string = c
End If
c.Offset(0, 1) = clean_string
Set c = c.Offset(1, 0)
Loop
End Sub
THe problem is when it finds a character in the array that matches the current string iteration, it ends up copying that string over to every cell iteration thereafter.
Here’s an example of output:
Owner1 Owner1
BURDINES 1225 LLC BURDINES 1225 LLC
MIAMI-DADE COUNTY MIAMI-DADE COUNTY
RELATED GROUP OF FLORIDA & RELATED GROUP OF FLORIDA
BURDINES 1225 LLC RELATED GROUP OF FLORIDA
CITY OF MIAMI DEPT OF P & D RELATED GROUP OF FLORIDA
MIAMI-DADE COUNTY RELATED GROUP OF FLORIDA
FEC R R CO RELATED GROUP OF FLORIDA
BDG 200 SOUTH MIAMI AVE LLC RELATED GROUP OF FLORIDA
STATE OF FLORIDA DOT RELATED GROUP OF FLORIDA
It copies over RELATED GROUP OF FLORIDA during each iteration after it finds that that cell ended with an & which was part of our end_string array. Same thing may be occuring with the substring array. I looked at this closely, and I am not sure why this behavior occurs. It’s supposed to be removing all characters in the cell that were found in both the end_string and substring array and copying them to new cell. If the indexes from the array were not found in the string, then it just copies over original string to column B. Also I use the string “End Loop” because I cannot check for an empty string since there may be 4 or 5 empty cells in column a before there is another cell with content, so I dont want the iteration to just stop just because one cell was empty.
Thanks for response
You need to wipe cleanerstring at the beginning of every pass through the loop. Otherwise, it retains its value from the last time through, and if the cell the doesn’t meet the criteria for cleanerstring to be edited, cleanstring becomes cleanerstring (which was the last iteration’s clearnerstring), so cleanstring will never be “”. Does that make sense?
EDIT
To make this clearer:
If the cell you’re looking at doesn’t meet the criteria for editing, cleanerstring is never set to anything new. So if was given a value the last time through, it still has that value. Thus, when you say
if cleanerstring was never set to anything new, cleanstring now has the cleanerstring value from the last time through. Cleanstring now gets cleaned up in your formatting code, and when you then check if
you evaluate to false, because cleanstring is last time’s cleanerstring, cleaned-up again. Thus cleanstring is not reset to c, so
sets the cell to the cleaned-up text from last time.