Search code examples
vbaexcelfor-loopvlookupgetelementsbytagname

VBA - “Run-time error 424: object required”


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 most 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 the point where the VB code would take over and 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 in 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 (Column H)
GenderText = dbsheet.Cells(SelRow, 8)

'Names (Column A)
NamesText = dbsheet.Cells(SelRow, 1)

'Loop Routine
For Row = 2 To lr
If NamesText.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
            theGenderText.Value = "F"
       ElseIf Doc.getElementsByTagName("b").Item(1).innerText = "It's a girl!" Then
            GenderText.Value = "M"
       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.


Solution

  • I got it! It was mostly a matter of changing GenderText and NamesText to range type variables. However, I may have other questions in the future as I add new features to the code. ;)