Sharid
Well-known Member
- Joined
- Apr 22, 2007
- Messages
- 1,066
- Office Version
- 2016
- Platform
- Windows
Hi
I have a code that works in taking href from a site, I now want to incorporate an If statement into the code, so if the data is there, then extract it and if not the place the word "nil" into the cell. Something like what I have below, but I can't get it to work. I have been stuck on this for a few days now and I can not seem to work it out.
The Full working code for the href is here
I have a code that works in taking href from a site, I now want to incorporate an If statement into the code, so if the data is there, then extract it and if not the place the word "nil" into the cell. Something like what I have below, but I can't get it to work. I have been stuck on this for a few days now and I can not seem to work it out.
VBA Code:
For Each link In htmlDoc.getElementsByTagName("a")
If link.getAttribute("class") = "vip" Then
Cells(i, 1).Value = link.getAttribute("href")
i = i + 1
End If
If link.getAttribute("class") = "lvtitle" & innerText = "" Then
Cells(i, 2).Value = "nil"
i = i + 1
Else
If link.getAttribute("class") = "lvtitle" Then
Cells(i, 2).Value = link.getAttribute("innertext")
i = i + 1
End If
If link.getAttribute("class") = "hotness-signal red" & innerText = "" Then
Cells(i, 3).Value = "nil"
i = i + 1
Else
If link.getAttribute("class") = "hotness-signal red" Then
Cells(i, 3).Value = link.getAttribute("innertext")
i = i + 1
End If
If link.getAttribute("class") = "prRange" & innerText = "" Then
Cells(i, 4).Value = "nil"
i = i + 1
Else
If link.getAttribute("prRange") = "lvtitle" Then
Cells(i, 4).Value = link.getAttribute("innertext")
i = i + 1
End If
Next link
The Full working code for the href is here
VBA Code:
Private Sub CommandButton3_Click()
Dim ie As Object
Dim htmlDoc As Object
Dim nextPageElement As Object
Dim div As Object
Dim link As Object
Dim URL As String
Dim pageNumber As Long
Dim i As Long
' Takes Url from Sheet2 A1 seach from Keyword from B1 and places IE
URL = Sheets("Sheet1").Range("A2").Value & Replace(Worksheets("Sheet1").Range("B2").Value, " ", "+")
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate URL
Do While .Busy Or .readyState <> 4
DoEvents
Loop
End With
Application.Wait Now + TimeSerial(0, 0, 5)
Set htmlDoc = ie.document
pageNumber = 1
i = 4
Do
For Each link In htmlDoc.getElementsByTagName("a")
If link.getAttribute("class") = "vip" Then
Cells(i, 1).Value = link.getAttribute("href")
i = i + 1
End If
Next link
' Clicks and goes to next page
If pageNumber >= 1 Then Exit Do
Set nextPageElement = htmlDoc.getElementsByClassName("gspr next")(0)
If nextPageElement Is Nothing Then Exit Do
nextPageElement.Click 'next web page
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set htmlDoc = ie.document
pageNumber = pageNumber + 1
Loop
ie.Quit
Set ie = Nothing
Set htmlDoc = Nothing
Set nextPageElement = Nothing
Set div = Nothing
Set link = Nothing
MsgBox "All Done"
End Sub