Hi Mike, Thanks alot for pointing me in the right direction. Sorry for not being clear -- I did want to parse the actual name parts into separate columns. I haven't had to program in VBA for quite a few years and it's changed a bit (no surprise). The real nut to crack was the fact that my input data is not consistently formatted. Some have no middle initial (and so only 1 space in the name), some have one letter MidInit, some have letter + period, some have the MidIniti appended the the first or last name with an underscore, etc. Getting the first name was no problem, but getting the MidInit correctly and then the last name correctly was more challenging. Thanks to your help, here is the ugly final solution. You have the run the MidInit subroutine before you run the LastName sub in order to get the last name out correctly.
Code:
Dim IsLastRow, counter, MiCount As Integer
Dim MiChar, MiLastChar, LastCellAddress As String
Sub ParseFirst()
IsLastRow = ActiveSheet.UsedRange.Rows.Count
For counter = 2 To IsLastRow
' Parse out first name from Column B PayOriginal sheet into Sheet1.Column B
ActiveSheet.Range("D" & counter).Value = Left(ActiveSheet.Range("B" & counter).Value, InStr(ActiveSheet.Range("B" & counter), " ") - 1)
Next
End Sub
Sub ParseMiddle()
IsLastRow = ActiveSheet.UsedRange.Rows.Count
For counter = 2 To IsLastRow
MiChar = ""
MiCount = 0
' Parse out Middle Initial, if there is one, into Sheet1.Column C
MiChar = Mid(ActiveSheet.Range("B" & counter).Value, InStr(ActiveSheet.Range("B" & counter).Value, " ") + 1, InStr(InStr(ActiveSheet.Range("B" & counter).Value, " ") + 1, ActiveSheet.Range("B" & counter).Value, " ") - InStr(ActiveSheet.Range("B" & counter).Value, " ") - 1)
On Error Resume Next
MiCount = Len(MiChar)
'Check if the last character in MiChar is a period, if it is, put the whole thing in sheet1.Column C as MI
MiLastChar = Right(MiChar, 1)
If MiCount > 1 And Asc(MiLastChar) = 46 Then
ActiveSheet.Range("E" & counter).Value = MiChar
ElseIf MiCount = 1 Then
ActiveSheet.Range("E" & counter).Value = MiChar
Else
ActiveSheet.Range("E" & counter).Value = ""
End If
MiChar = ActiveSheet.Range("E" & counter).Value
On Error Resume Next
ActiveSheet.Range("E" & counter).Value = RTrim(MiChar)
Next
End Sub
Sub ParseLast()
IsLastRow = ActiveSheet.UsedRange.Rows.Count
For counter = 2 To IsLastRow
'If text after 1st space is not MI, then extract Last Name begining from 1st space and put into Sheet1.ColumnA, else extract last name beginning from 2nd space into Sheet1.ColumnA
If ActiveSheet.Range("E" & counter).Value = "" Then
ActiveSheet.Range("C" & counter).Value = Right(ActiveSheet.Range("B" & counter).Value, Len(ActiveSheet.Range("B" & counter).Value) - InStr(1, ActiveSheet.Range("B" & counter).Value, " ", vbCompareText) + 1)
Else
ActiveSheet.Range("C" & counter).Value = Right(ActiveSheet.Range("B" & counter).Value, Len(ActiveSheet.Range("B" & counter).Value) - InStr(InStr(1, ActiveSheet.Range("B" & counter).Value, " ", vbTextCompare) + 1, ActiveSheet.Range("B" & counter).Value, " ", vbTextCompare))
End If
MiChar = ActiveSheet.Range("C" & counter).Value
ActiveSheet.Range("C" & counter).Value = Trim(MiChar)
Next
End Sub
I was getting a 5 error when it hit a name with only one space in it, thus the ugly error handling.
Sigh. It ain't pretty, but it works. Now I have to do a two-sheet compare with lots of convoluted selection criteria. double sigh.
Thanks again Mike! -- jon