Gurus,
I have scoured the internet for a solution.
Ultimately I am trying to create a macro of which the user enters a new employees first and last name. The macro places that in column "B" and in row by alphabetic order.
User enters: Cosmo Kramer
List (column B)
George Constanza
Jerry Steinfeld
Result
George Constanza
Cosmo Kramer
Jerry Steinfeld
I am using bits and pieces of code I found but I cannot figure out how to define LastnameLP and FirstnameLP variables.
Please help, or let me know if there is an easier way. I need to keep the first and last name in the same cell.
Tia,
Tim
Sub Macro2()
Dim Firstname As String, Surname As String, Namestr As String, FirstnameLP As String, LastnameLP As String, i As Integer, TempRng As Range, r As Integer
Namestr = InputBox("Please enter the new employees' name below.", "New Employee")
If Not Namestr = "" Then
Firstname = Left(Namestr, InStr(Namestr, " ") - 1)
Surname = Right(Namestr, Len(Namestr) - Len(Firstname) - 1)
Else
MsgBox ("Please provide a valid name.")
Exit Sub
End If
For i = 4 To 100
'FirstnameLP = **the first name in Cell(B(i))
'LastnameLP = **the last name in Cell(B(i))
If LastnameLP > Surname Then
Range(i & ":" & i).EntireRow.Insert
Cells(i, 2).Value = Firstname & " " & Surname
r = r + 1
Exit For
ElseIf LastnameLP = Surname Then
If FirstnameLP > Firstname Then
Range(i & ":" & i).EntireRow.Insert
Cells(i, 2).Value = Firstname & " " & Surname
r = r + 1
Exit For
End If
End If
Next i
'in case you should insert at end of list
If Cells(i, 1).Value = vbNullString Then
Cells(i, 2).Value = Firstname & " " & Surname
End If
End Sub
Bookmarks