I'm writing some VBA code that should search for certain words in the Address1 field (e.g., "Suite", "Ste", "Apt") then copies the word and what follows it to the Address2 column. I also need it to truncate the Address1 value so that the word and what follows it no longer appears there.
For example: Address1 = "1234 Park Ave Ste 234"
The code should find "Ste" in the cell's value then copy "Ste 234" into the Address2 column. Address1 should then be changed to "1234 Park Ave".
The code should then recycle and search Address1 again for the word "Suite" and then do the same thing.
Here's the code I have for "Ste":
With xlWs.Range("L2:L" & xlWs.Cells.SpecialCells(xlLastCell).row)
Set FindRow = .Find(What:="Ste", LookAt:=xlPart, SearchOrder:=xlByRows, LookIn:=xlValues)
If Not FindRow Is Nothing Then
Set FirstRow = FindRow
xlWs.Range("M" & FindRow.row).Formula = "=Right(L" & FindRow.row & ", LEN(L" & FindRow.row & ")-Search(""Ste"", L" & FindRow.row & ")+1)"
Do
xlWs.Range("M" & FindRow.row).Formula = "=Right(L" & FindRow.row & ", LEN(L" & FindRow.row & ")-Search(""Ste"", L" & FindRow.row & ")+1)"
Set FindRow = .FindNext(after:=FindRow)
Loop While Not FindRow Is Nothing And FindRow <> FirstRow
End If
End With
It seems to work for my first search for "Suite" but then doesn't work for the other words. Maybe I have to get it to the first cell in the column to start the search again.
Does anyone have any ideas about this? Does the code look correct?
For example: Address1 = "1234 Park Ave Ste 234"
The code should find "Ste" in the cell's value then copy "Ste 234" into the Address2 column. Address1 should then be changed to "1234 Park Ave".
The code should then recycle and search Address1 again for the word "Suite" and then do the same thing.
Here's the code I have for "Ste":
With xlWs.Range("L2:L" & xlWs.Cells.SpecialCells(xlLastCell).row)
Set FindRow = .Find(What:="Ste", LookAt:=xlPart, SearchOrder:=xlByRows, LookIn:=xlValues)
If Not FindRow Is Nothing Then
Set FirstRow = FindRow
xlWs.Range("M" & FindRow.row).Formula = "=Right(L" & FindRow.row & ", LEN(L" & FindRow.row & ")-Search(""Ste"", L" & FindRow.row & ")+1)"
Do
xlWs.Range("M" & FindRow.row).Formula = "=Right(L" & FindRow.row & ", LEN(L" & FindRow.row & ")-Search(""Ste"", L" & FindRow.row & ")+1)"
Set FindRow = .FindNext(after:=FindRow)
Loop While Not FindRow Is Nothing And FindRow <> FirstRow
End If
End With
It seems to work for my first search for "Suite" but then doesn't work for the other words. Maybe I have to get it to the first cell in the column to start the search again.
Does anyone have any ideas about this? Does the code look correct?