简体   繁体   中英

Runtime error 91 and 1004 VBA

I'm new to VBA. I've been trying to get the value "3.2500" into cell B1. So far I'm getting runtime error 1004 and error 91 -object variable or with block variable not set- at times. What could be causing that? Its a Clean workbook.

Sub extract()

Dim myIE As Object
Dim myIEDoc As Object
Dim element As IHTMLElement

Set myIE = CreateObject("InternetExplorer.Application")

 myIE.Visible = False

myIE.navigate "https://zonasegura1.bn.com.pe/TipoCambio/"

While myIE.Busy
    DoEvents
Wend
 Set myIEDoc = myIE.document

Range("B1") = myIEDoc.getElementsByClassName("movimiento")(0).getElementsByClassName("l2 valor")(0)

End Sub

your issue is:

 Range("B1") = myIEDoc.getElementsByClassName("movimiento")(0).getElementsByClassName("l2 valor")(0)

Is ambiguous. You need to fully specify where what is going. "Range this or that" is meaningless if you dont say what workbook or what worksheet they go into.

The getElementsByID("movimiento") is considering class="movimiento bg" a match and there is no child element that matches so you are trying to get .innerText from nothing. You need the class name's second match and then locate the child or look for class="l2 valor" directly since the one you want is the third in the document.

'option 1
Range("B1") = myIEDoc.getElementsByClassName("movimiento")(1).getElementsByClassName("l2 valor")(0).innertext
'option 2
Range("B1") = myIEDoc.getElementsByClassName("l2 valor")(2).innertext

Remember that the index numbers of a collection of elements are zero-based so to get the third you ask for index number (2).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM