Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA运行时错误91 can';解决不了_Excel_Runtime Error_Excel 2010_Vba - Fatal编程技术网

Excel VBA运行时错误91 can';解决不了

Excel VBA运行时错误91 can';解决不了,excel,runtime-error,excel-2010,vba,Excel,Runtime Error,Excel 2010,Vba,我正在尝试使用XML从网站中提取数据。请看下面我的脚本: 剧本 Option Explicit Private Sub btnRefresh_Click() Dim req As New XMLHTTP Dim resp As New DOMDocument Dim weather As IXMLDOMNode Dim ws As Worksheet: Set ws = ActiveSheet Dim wshape As Shape Dim th

我正在尝试使用XML从网站中提取数据。请看下面我的脚本:

剧本

Option Explicit

Private Sub btnRefresh_Click()
    Dim req As New XMLHTTP
    Dim resp As New DOMDocument
    Dim weather As IXMLDOMNode
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim wshape As Shape
    Dim thiscell As Range
    Dim i As Integer

    req.Open "GET", "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=myConfidentialToken&q=Baku&format=xml&num_of_days=5"
    req.send

    resp.LoadXML req.responseText

    For Each weather In resp.getElementsByTagName("weather")
        i = i + 1
        ws.Range("theDate").Cells(1, i).Value = weather.SelectNodes("date")(0).Text
        ws.Range("highTemps").Cells(1, i).Value = weather.SelectNodes("maxtempC")(0).Text
        ws.Range("lowTemps").Cells(1, i).Value = weather.SelectNodes("mintempC")(0).Text
        Set thiscell = ws.Range("weatherPicture").Cells(1, i)
        Set wshape = ws.Shapes.AddShape(msoShapeRectangle, thiscell.Left, thiscell.Top, thiscell.Width, thiscell.Height)
        wshape.Fill.UserPicture weather.SelectNodes("weatherIconUrl").Item(0).Text
    Next weather

End Sub

此脚本返回运行时错误91。它表示:“未设置对象变量或带块变量”。调试时,运行会在“next weather”语句之前的行停止。为什么这个脚本返回这样一个错误,而我已经设置了“wshape”变量?

正如@JohnRC所指出的,每个
节点中都有
节点集,而每个
节点依次包含
。您可以将响应XML保存到文件中,并使用任何联机XML树查看器查看结构:

请看下面的示例,添加了一个嵌套循环以从每个
节点获取

选项显式
私有子btnRefresh\u单击()
作为对象的Dim oReq
作为字符串的Dim sResp
作为对象的Dim-oDoc
作为对象的Dim-oData
将日期作为对象
以今天为对象
模糊的鼻孔作为物体
作为对象的模糊oHour
设置oReq=CreateObject(“MSXML2.XMLHTTP”)
oReq.打开“获取”http://api.worldweatheronline.com/premium/v1/weather.ashx?key=e11be04676ad49038f9175720181600&q=saint-彼得堡&format=xml&num_of_days=5“,假
oReq.发送
sResp=oReq.ResponseText
设置oDoc=CreateObject(“MSXML2.DOMDocument”)
oDoc.LoadXML sResp
Set oData=oDoc.getElementsByTagName(“数据”)(0)
设置cDays=oData。选择节点(“天气”)
每一天(以cDays为单位)
调试。打印日期。选择节点(“日期”)(0)。文本
Debug.Print oDay.SelectNodes(“maxtempC”)(0.Text
Debug.Print oDay.SelectNodes(“mintempC”)(0.Text
设置cHours=oDay.SelectNodes(“每小时”)
每一个秋千
调试。打印vbTab&oHour。选择节点(“时间”)(0)。文本
Debug.Print vbTab&oHour.SelectNodes(“tempC”)(0.Text
Debug.Print vbTab&oHour.SelectNodes(“weatherIconUrl”)(0)。Text
下一个
下一个
端接头

错误可能发生在前一行-Set wshape=ws.Shapes…该行工作正常,并将矩形添加到“thiscell”。问题出现在“wshape.Fill…”行中。我怀疑
weather.SelectNodes(“weatherIconUrl”)…
没有返回任何内容,因此尝试获取项目(0)。文本失败。创建一个变量并首先将变量设置为该对象,然后检查它是否为空(
Debug.Print TypeName(…)
很有用),因此wshape肯定是一个对象?查看WorldWeatherOnline文档,该元素似乎是或元素的子元素,而不是的直接子元素,因此,您可能会从
weather.SelectNodes(“weathericonur”)
中得到一个空的设置。这是真的。我实现的XML树是错误的,但我不认为这会返回这样的错误。谢谢你的帮助!