So although I feel the need to mention that I’ve already made several posts on here regarding this project, I’ve since made some progress on it:
For those that haven’t read any of my prior posts, I have a database of names for an online membership site, for some of which the genders have been assigned with VLOOKUP from a 6,000 name long list on another sheet. For the rest, they come up as “ERR”, which is where the VB code would search up the name on a gender guesser website and be able to return an “F”, “M”, or ‘U” (unknown) for those cells. The function I used all the cells throughout the gender column is as follows:
=IF(ISERROR(VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE)),"ERR",VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE))
And the VBA code to take care of the rest is as follows:
Sub DetermineGender()
Dim dbsheet As Worksheet
Set dbsheet = ThisWorkbook.Sheets("memberdata2")
lr = dbsheet.Cells(Rows.Count, 1).End(xlUp).Row
SelRow = Selection.Row
'Gender
GenderText = dbsheet.Cells(SelRow, 8)
'Names
NamesText = dbsheet.Cells(SelRow, 1)
'Loop Routine
For Row = 2 To lr
If GenderText.Value = "ERR" Then
Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://www.gpeters.com/names/baby-names.php?name=" & NamesText
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.Document
If Doc.getElementsByTagName("b").Item(1).innerText = "It's a boy!" Then
GenderText.Value = "M"
ElseIf Doc.getElementsByTagName("b").Item(1).innerText = "It's a girl!" Then
GenderText.Value = "F"
Else
GenderText.Value = "U"
End If
End If
Next
End Sub
I’m pretty much an absolute beginner at VBA, and most of my code was cobbled together from 4-5 different sources. Although I mentioned that I was getting the “Run-time error 424: object required” message, I am fairly sure that other errors exist in my code, so please feel free to point those out, as well.
Bookmarks