Extract first and last name from cell with two names or cell with double last names

nicnad

Board Regular
Joined
Sep 12, 2011
Messages
199
Hi,

I have some code to extract first and last names from a cell when double clicked and open an internet website and fill a form:

Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
 
    Cancel = True
    Dim myRangedoc As Range
    Set myRangedoc = Range("phonebookname")
    If Not Intersect(Target, myRangedoc) Is Nothing Then
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim stGotIt As String
    firstname = Left(Target.Value, InStr(1, Target.Value, " ", vbTextCompare))
    stGotIt = StrReverse(Target.Value)
    stGotIt = Left(stGotIt, InStr(1, stGotIt, " ", vbTextCompare))
    lastname = StrReverse(Trim(stGotIt))
 
    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
 
    ' You can uncoment Next line To see form results
    IE.Visible = False
 
    ' Send the form data To URL As POST binary request
    IE.Navigate "http://mywebsite.com"
 
    ' Statusbar
    Application.StatusBar = "Loading. Please wait..."
 
    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
  
    Application.StatusBar = "Search form submission. Please wait..."
    Set objCollection = IE.document.getElementsByTagName("input")
 
    i = 0
    While i < objCollection.Length
 
        If objCollection(i).Name = "searchForm.lastName" Then
 
            ' Set text for search
            objCollection(i).Value = lastname
 
        ElseIf objCollection(i).Name = "searchForm.firstName" Then
 
            ' Set text for search
            objCollection(i).Value = firstname
 
        Else
            If objCollection(i).Type = "submit" Then
 
                ' "Search" button is found
                Set objElement = objCollection(i)
 
            End If
        End If
        i = i + 1
    Wend
    objElement.Click    ' click button to search
 
    ' Wait while IE re-loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
 
    ' Show IE
    IE.Visible = True
 
    ' Clean up
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
 
    Application.StatusBar = ""
End If
End Sub

This code works great for simple name as "Barrack Obama" in a cell.

The thing is I sometimes have two names in a cell separated by a backslash (i.e. "Barrack Obama / Ben Bernanke")
I also have errors when there is a double last name (i.e. "George W. Bush")
When there is two names in a cell, I would like to have a listbox pop up and have the user choose the name he wants to search on the website and then I would like the variable firstname and lastname to be the one selected by the user.
When there is more than one last name I would like the variable lastname to be both last names (i.e. "W. Bush").

Can you please help me?

Thank you.
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top