Search code examples
excelvbaweb-scrapingdata-extraction

Error in compiling in VBA "set-defined type not defined"


While compiling the code in VBA I'm facing an error was

"user-defined type not defined"

Can anyone help me to solve that issue?

Sub import_ZipRecruiter()

Dim IE As SHDocVw.InternetExplorer
Set IE = New InternetExplorer
 IE.Visible = True
IE.Navigate "https://www.ziprecruiter.com/candidate/search? 
search=ax&location=USA&days=5&refine_by_salary=&refine_by_tags=&refine_by_title=&refine_by_org_name="

Do While IE.ReadyState <> READYSTATE_COMPLETE

DoEvents
Loop

Sheets("Sheet3").Select
Cells.Select
Selection.ClearContents



Dim idoc As HTMLDocument
Set idoc = IE.Document
Dim r As Long, c As Long

Dim just_job_title As IHTMLElement
Dim dts As IHTMLCollection
Set dts = idoc.getElementsByClassName("just_job_title")
c =1 ; r=1

For Each just_job_title In dts

Sheets("Sheet3").Cells(r, c) = just_job_title.innerText
c = c + 1
Next just_job_title

End Sub

Solution

  • Try the following to get the desired output. This time you should not require any reference to the libraries to execute the script.

    Sub GetJobTitles()
        Const Url$ = "https://www.ziprecruiter.com/candidate/search?%20%20search=ax&location=USA&days=5&refine_by_salary=&refine_by_tags=&refine_by_title=&refine_by_org_name="
        Dim post As Object, R&
    
        With CreateObject("InternetExplorer.Application")
            .Visible = True
            .navigate Url
            While .Busy Or .readyState < 4: DoEvents: Wend
            For Each post In .document.getElementsByTagName("article")
                R = R + 1: Cells(R, 1) = post.getElementsByClassName("just_job_title")(0).innerText
                Cells(R, 2) = post.getElementsByClassName("name")(0).innerText
                Cells(R, 3) = post.getElementsByClassName("location")(0).innerText
            Next post
            .Quit
        End With
    End Sub