The change to instr now gives me a "type mismatch" error highlighting this line:
middle_name = Mid(B4, InStr(" ", B4) + 1, InStr(" ", B4, 1 + InStr(" ", B4)) - InStr(" ", B4) - 1)
with this code:
Sub Inspect()
Dim old_str As String
Dim new_str As String
Dim first_name As String
Dim last_name As String
Dim middle_name As String
Dim regex As Object
Dim Rng As Range
Dim InitialRange As Variant
Dim CompareRange As Variant
Dim x As Variant
Dim y As Variant
ReDim KeepThese(1 To 10000)
pos = InStr(Range("A1"), "AKA")
If pos <> 0 Then
old_str = Right(Range("A1"), Len(Range("A1")) - pos - 2)
new_str = Left(old_str, Len(old_str) - 1)
Range("A1").Value = Trim(new_str)
Set Rng = Range("A1")
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.Pattern = "(^\w+\s\w+\s\w+)"
If regex.Test(Range("A1").Value) Then
first_name = Left(B1, InStr(" ", B1) - 1)
middle_name = Mid(B4, InStr(" ", B4) + 1, InStr(" ", B4, 1 + InStr(" ", B4)) - InStr(" ", B4) - 1)
last_name = Right(A1, Len(A1) - InStr("*", WorksheetFunction.Substitute(A1, " ", "*", Len(A1) - Len(WorksheetFunction.Substitute(A1, " ", "")))))
End If
Range("A1").Value = last_name & " " & first_name & " " & middle_name
End If
Set InitialRange = Range("A:A")
Set CompareRange = Range("B:B")
For Each x In InitialRange
For Each y In CompareRange
If x = y Then
KeepThese = IIf(Len(x) > Len(y), x, y)
End If
Next y
Next x
For Each Key In KeepThese
Range("C1").Value = Key
Next Key
End Sub
Note that now I am just targeting A1, for example, but obviously I will need to change that to A1 & i because I would be iterating the length of the column. I just do this now so it makes it easier to troubleshoot.
Basically, in cell A1 I have something like this:
In property of AKA Jen S Stephen,
In cell B1, I have something like this:
So my VBA above intends to change A1 to this: Stephen Jen S and then which ever is greater length (e.g. Stephen Jen S or Stephen Jen) output it to cell C1.
Thanks for response.
Bookmarks