Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Error 91 while getting data from website

0

First of all i'm using this code from https://www.teachexcel.com/excel-tutorial/2189/vba-macro-to-import-web-data-into-excel

It runs well in yahoo finance website, no problem at all

Then I modify the website url and the class name to suit my need, but it runs into error 91 Object Variable or With Block Variable Not Set

The website i'm trying is "https://item.taobao.com/item.htm?id=531457661561"

The classname of text i'm trying to get is "tb-rmb-num"

The whole code is as follow, any helps are very appreciated

Sub Get_Price()

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String


' Website to go to.
website = "https://item.taobao.com/item.htm?id=531457661561"

' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.ServerXMLHTTP.6.0")

' Where to go and how to go there - probably don't need to change this.
request.Open "GET", website, False


' Send the request for the webpage.
request.send

' Get the webpage response data into a variable.
response = StrConv(request.responseBody, vbUnicode)

' Put the webpage into an html object to make data references easier.
html.body.innerHTML = response

' Get the price from the specified element on the page.
price = html.getElementsByClassName("tb-rmb-num")(0).innerText

' Output the price into a message box.
MsgBox price

End Sub
Answer
Discuss

Answers

0

Hi and welcome to the Forum

Sounds like you need to install the required Reference library. In VB Explorer menu, go Tools/References... and tick/check the "Microsoft HTML Object Library" (then OK to perform).

If that doesn't work, try my previous answer: Run time error 91

Hope this helps

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login