0

Im looking to create a VBA script which checks a website and pulls the price from the class. My issue there are 2 potential classes ".kuSalePrice kuSpecialPrice" and ".kuSalePrice". I created the below to check if the class ".kuSalePrice" exists and if not, the second class and output the price.

I am getting the following error on the line below

Worksheets("Sheet1").Range("C" & lRow).Value = result2.innerText

"Object variable or with block variable not set"

Sub PriceCheck()

Dim ie As InternetExplorer
Dim doc As HTMLDocument
Dim result As IHTMLElement
Dim result2 As IHTMLElement
Dim url As String
Dim lRow As Long

Set ie = New InternetExplorer

ie.Visible = True
lRow = 2
url = Worksheets("Sheet1").Range("B" & lRow).Value

Do Until url = ""
    ie.navigate url

    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    Set doc = ie.document

    Set result = doc.querySelector(".SalePrice")


    If IsObject(doc.querySelector(".SalePrice")) Then
    MsgBox "does exsist"
    Set result2 = result

    Else
    MsgBox "doesnt exsist"
    Set result2 = doc.querySelector(".SalePrice SpecialPrice")

    End If

    Worksheets("Sheet1").Range("C" & lRow).Value = result2.innerText

    lRow = lRow + 1
    url = Worksheets("Sheet1").Range("B" & lRow).Value
Loop

Cells.Replace What:="£", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False

End Sub
Community
  • 1
  • 1
Jess Murray
  • 1,273
  • 1
  • 10
  • 32
  • 1
    Can you share the URL? – QHarr May 10 '18 at 19:21
  • Try to implement the approach described [here](https://stackoverflow.com/a/32170074/2165759) and [here](https://stackoverflow.com/a/47084865/2165759). – omegastripes May 10 '18 at 19:47
  • "IsObject returns True even if the variable has been set to Nothing. Use error trapping to be sure that an object reference is valid" - from the VBA reference for `IsObject` found [here](https://msdn.microsoft.com/en-us/VBA/Language-Reference-VBA/articles/isobject-function) – barrowc May 10 '18 at 23:35

1 Answers1

0

I didn't test it but I would try something like :

If IsObject(doc.querySelector(".SalePrice")) Then
    Set result = doc.querySelector(".SalePrice")
    Worksheets("Sheet1").Range("C" & lRow).Value = result.innerText
ElseIf IsObject(doc.querySelector(".SalePrice SpecialPrice")) Then
    Set result = doc.querySelector(".SalePrice SpecialPrice")
    Worksheets("Sheet1").Range("C" & lRow).Value = result.innerText
End If
DanB
  • 2,022
  • 1
  • 12
  • 24