johnmerlino
Board Regular
- Joined
- Sep 21, 2010
- Messages
- 94
Hey all,
I'm having a little difficulty with a vba macro I am creating in excel.
Basically, item in column A may also appear in column B. I want the one with the greatest length in column C (In other words, Jennifer T Tamashiro is the same person as Tamashiro, Jennifer but since the first has a T middle initial, I would like to preserve that in column C and not Tamashiro, Jennifer) . However, if it does not appear in column B, then since there is njothing to compare, so for ones where the first and last names don't match a cell in column B or a cell in column A, I would like that output in Column C. Now there is one special case. Some names in column B will have something like: In trust of AKA Stephen T Smith, for example. In that case, I want to strip the comma at end and extract to the right of AKA so I only get the name and then do a comparison with the other column to see if the first and last name are the same and if so keep the one with middle initial. Also note that the ones in B always have last comma first except for the ones with AKA are first middle last. I started building the macro in VBA, had a little assistance, but still it's not right. It doesn't pull unique names in column c and it pulls all names in column C as first middle last rather than last first middle. Also on some instances it cuts away a portion of the last name I think. Here's an example of what I would like to have:
Here's the macro that is causing problems:
Thanks for response
I'm having a little difficulty with a vba macro I am creating in excel.
Basically, item in column A may also appear in column B. I want the one with the greatest length in column C (In other words, Jennifer T Tamashiro is the same person as Tamashiro, Jennifer but since the first has a T middle initial, I would like to preserve that in column C and not Tamashiro, Jennifer) . However, if it does not appear in column B, then since there is njothing to compare, so for ones where the first and last names don't match a cell in column B or a cell in column A, I would like that output in Column C. Now there is one special case. Some names in column B will have something like: In trust of AKA Stephen T Smith, for example. In that case, I want to strip the comma at end and extract to the right of AKA so I only get the name and then do a comparison with the other column to see if the first and last name are the same and if so keep the one with middle initial. Also note that the ones in B always have last comma first except for the ones with AKA are first middle last. I started building the macro in VBA, had a little assistance, but still it's not right. It doesn't pull unique names in column c and it pulls all names in column C as first middle last rather than last first middle. Also on some instances it cuts away a portion of the last name I think. Here's an example of what I would like to have:
Code:
JENNIFER T TAMASHIRO TAMASHIRO, JENNIFER
JORGE J GARCIA STANIC, ZORAN
Jamie Smith In honor of AKA Jamie L Smith
Based on above example, what I would like in column C:
TAMASHIRO JENNIFER T
GARCIA JORGE J
ZORAN STANIC
Smith Jamie L
Here's the macro that is causing problems:
Code:
Option Explicit
Sub ParseNames()
Dim AKA_Pos As Long, _
ctrl As Long, _
LastRow As Long, _
TestCell As Range, _
NewString As String, _
Fragments As Variant
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each TestCell In Range("A1:A" & LastRow)
AKA_Pos = InStr(UCase(TestCell.Value), " AKA ") + 5
Fragments = Split(Mid(TestCell.Value, AKA_Pos), " ")
For ctrl = 0 To UBound(Fragments)
Fragments(ctrl) = WorksheetFunction.Substitute(Fragments(ctrl), ",", "")
Next ctrl
NewString = Fragments(UBound(Fragments))
For ctrl = 0 To UBound(Fragments) - 1
NewString = NewString & " " & Fragments(ctrl)
Next ctrl
NewString = Trim(NewString)
Select Case Len(NewString) > Len(Cells(TestCell.Row, "B").Value)
Case Is = True
Cells(TestCell.Row, "C").Value = NewString
Case Is = False
Cells(TestCell.Row, "C").Value = Cells(TestCell.Row, "B").Value
End Select
Next TestCell
End Sub
Thanks for response