1

So I'm new to vba and i am try to get price (i tried everyting my know). The macro is :

Sub Deneme()

Dim objIE As InternetExplorer
Dim Prc1 As String
Set objIE = New InternetExplorer
Dim Search_Terms() As Variant
Dim CopiedData() As Variant


Dim y As Integer
objIE.Visible = False


Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A169").Value)

ReDim CopiedData(LBound(Search_Terms) To UBound(Search_Terms))


y = 2
For a = LBound(Search_Terms) To UBound(Search_Terms)


objIE.navigate "https://steamcommunity.com/market/listings/578080/" & Search_Terms(a)
Do: DoEvents: Loop Until objIE.readyState = 4
Prc1 = objIE.document.getElementsByClassName("market_commodity_orders_table")(4).getElementsByTagName("tr")(1).textContent '<----- the problem is here
ActiveSheet.Range("D" & y).Value = Prc1

y = y + 1
Next


objIE.Quit

End Sub

The website is THIS and I am trying to get this value: a

Mostly error is :

Run-time error '91': Object variable or With block variable not set.

And Debug is :

objIE.document.getElementsByClassName("market_commodity_orders_table")(4).getElementsByTagName("tr")(1).textContent

3 Answers 3

1

In the process of me testing my new code for you, I realized that you have other issues other than the class name you were attempting to use not existing.

The other issue is that the document loads before some of the other resources - this is likely due to the fact that this site updates the price every second (and therefore the price is not initially loaded in the objIE.Document object).

To get around this, I've added a couple of loops to wait for your object to become available. This should work for you.

Sub Deneme()

    Dim objIE As InternetExplorer
    Dim Prc1 As String
    Set objIE = New InternetExplorer
    Dim Search_Terms() As Variant
    Dim CopiedData() As Variant
    Dim y As Integer
    Dim elemObj As Object

    objIE.Visible = False

    Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A169").Value)

    ReDim CopiedData(LBound(Search_Terms) To UBound(Search_Terms))

    y = 2
    For a = LBound(Search_Terms) To UBound(Search_Terms)

        objIE.navigate "https://steamcommunity.com/market/listings/578080/" & Search_Terms(a)
        Do: DoEvents: Loop Until objIE.readyState = 4

        Do While Prc1 = ""
            Do While elemObj Is Nothing
                Set elemObj = objIE.document.getElementById("market_commodity_buyrequests")
                Set elemObj = elemObj.getElementsByClassName("market_commodity_orders_header_promote")(1)
            Loop
            Prc1 = elemObj.innerText
        Loop

        ActiveSheet.Range("D" & y).Value = Prc1
        Set elemObj = Nothing
        Prc1 = vbNullString

        y = y + 1

    Next

    objIE.Quit

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

the result is only 5678. its wrong and its not working with Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A169").Value), pages are changing but the result is only 5678 for every search_terms
Thanks for the feedback. I added the line Prc1 = vbNullString as it's likely bypassing the Do...Loop in your code because it's not empty after the first iteration, thus defeating the purpose of the loop. See if that solves the issue.
1

There are 2 issues in your code …

  1. There is no class called market_commodity_orders_table
  2. item counting starts with 0 so the 4ᵗʰ item is item no 3.

You can use this:

Prc1 = objIE.document.getElementsByClassName("market_commodity_orders_header_promote").Item(3).innerText

3 Comments

Well, I tested it and it worked for the link in your question!
objIE.navigate "https://steamcommunity.com/market/listings/578080/" & Search_Terms(a) Do: DoEvents: Loop Until objIE.readyState = 4 Prc1 = objIE.document.getElementsByClassName("market_commodity_orders_header_promote").Item(3).innerText ActiveSheet.Range("D" & y).Value = Prc1 Do you use like this ?
yes and I used "PLAYERUNKNOWN's Bandana" as cell value.
1

Let us try it in a slightly different manner. If you have IE9 or later then the following code should work for you flawlessly. I used .querySelector() here. Give this a shot and find the price you are after.

Sub GetPrice()
    Const URL As String = "https://steamcommunity.com/market/listings/578080/PLAYERUNKNOWN's%20Bandana"
    Dim HTML As HTMLDocument, post As Object

    With New InternetExplorer
        .Visible = True
        .navigate URL
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document

        Do: Set post = HTML.querySelector("#market_commodity_buyrequests .market_commodity_orders_header_promote:nth-of-type(2)"): DoEvents: Loop While post Is Nothing

        [A1] = post.innerText
        .Quit
    End With
End Sub

Reference to add to the library:

Microsoft Internet Controls
Microsoft HTML Object Library

1 Comment

i am not using a single url. Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A169").Value) urls with using search_terms. objIE.navigate "https://steamcommunity.com/market/listings/578080/" & Search_Terms(a)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.