The below VBA script errors out when it reaches a node where the value is missing. For example, for most days, <precipitation> includes @VALUE. However, some days it does not and this is causing my script to error out. How can I tell the script to simply print 0 if it errors out and move on?
link to XML: http://api.openweathermap.org/data/...78079d2e2d49e35ddb&mode=xml&units=imperial&q=
VBA Script:
link to XML: http://api.openweathermap.org/data/...78079d2e2d49e35ddb&mode=xml&units=imperial&q=
VBA Script:
VBA Code:
Public Sub getWeather()
Dim xmlhttp As New MSXML2.ServerXMLHTTP, myurl As String, xmlresponse As New DOMDocument
myurl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=houston&cnt=7&appid=4a2360d14bf33378079d2e2d49e35ddb&mode=xml&units=imperial&q="
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
xmlresponse.LoadXML (xmlhttp.responseText)
'Houston 7-Day Weather Data
Worksheets("Weather").Range("B2").Value = UCase(xmlresponse.SelectNodes("//weatherdata/location/name")(0).Text)
Worksheets("Weather").Range("C3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(0).Text
Worksheets("Weather").Range("C4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(0).Text)
Worksheets("Weather").Range("C5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(0).Text)
Worksheets("Weather").Range("C6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(0).Text)
Worksheets("Weather").Range("C7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(0).Text
Worksheets("Weather").Range("C8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(0).Text
Worksheets("Weather").Range("C9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(0).Text
Worksheets("Weather").Range("C10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(0).Text
Worksheets("Weather").Range("C11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(0).Text
Worksheets("Weather").Range("C12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(0).Text
Worksheets("Weather").Range("C13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(0).Text
Worksheets("Weather").Range("D3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(1).Text
Worksheets("Weather").Range("D4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(1).Text)
Worksheets("Weather").Range("D5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(1).Text)
Worksheets("Weather").Range("D6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(1).Text)
Worksheets("Weather").Range("D7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(1).Text
Worksheets("Weather").Range("D8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(1).Text
Worksheets("Weather").Range("D9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(1).Text
Worksheets("Weather").Range("D10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(1).Text
Worksheets("Weather").Range("D11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(1).Text
Worksheets("Weather").Range("D12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(1).Text
Worksheets("Weather").Range("D13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(1).Text
Worksheets("Weather").Range("E3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(2).Text
Worksheets("Weather").Range("E4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(2).Text)
Worksheets("Weather").Range("E5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(2).Text)
Worksheets("Weather").Range("E6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(2).Text)
Worksheets("Weather").Range("E7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(2).Text
Worksheets("Weather").Range("E8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(2).Text
Worksheets("Weather").Range("E9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(2).Text
Worksheets("Weather").Range("E10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(2).Text
Worksheets("Weather").Range("E11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(2).Text
Worksheets("Weather").Range("E12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(2).Text
Worksheets("Weather").Range("E13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(2).Text
Worksheets("Weather").Range("F3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(3).Text
Worksheets("Weather").Range("F4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(3).Text)
Worksheets("Weather").Range("F5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(3).Text)
Worksheets("Weather").Range("F6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(3).Text)
Worksheets("Weather").Range("F7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(3).Text
Worksheets("Weather").Range("F8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(3).Text
Worksheets("Weather").Range("F9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(3).Text
Worksheets("Weather").Range("F10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(3).Text
Worksheets("Weather").Range("F11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(3).Text
Worksheets("Weather").Range("F12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(3).Text
Worksheets("Weather").Range("F13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(3).Text
Worksheets("Weather").Range("G3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(4).Text
Worksheets("Weather").Range("G4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(4).Text)
Worksheets("Weather").Range("G5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(4).Text)
Worksheets("Weather").Range("G6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(4).Text)
Worksheets("Weather").Range("G7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(4).Text
Worksheets("Weather").Range("G8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(4).Text
Worksheets("Weather").Range("G9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(4).Text
Worksheets("Weather").Range("G10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(4).Text
Worksheets("Weather").Range("G11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(4).Text
Worksheets("Weather").Range("G12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(4).Text
Worksheets("Weather").Range("G13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(4).Text
Worksheets("Weather").Range("H3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(5).Text
Worksheets("Weather").Range("H4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(5).Text)
Worksheets("Weather").Range("H5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(5).Text)
Worksheets("Weather").Range("H6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(5).Text)
Worksheets("Weather").Range("H7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(5).Text
Worksheets("Weather").Range("H8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(5).Text
Worksheets("Weather").Range("H9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(5).Text
Worksheets("Weather").Range("H10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(5).Text
Worksheets("Weather").Range("H11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(5).Text
Worksheets("Weather").Range("H12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(5).Text
Worksheets("Weather").Range("H13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(5).Text
Worksheets("Weather").Range("I3").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/@day")(6).Text
Worksheets("Weather").Range("I4").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/symbol/@name")(6).Text)
Worksheets("Weather").Range("I5").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@value")(6).Text)
Worksheets("Weather").Range("I6").Value = UCase(xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@name")(6).Text)
Worksheets("Weather").Range("I7").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@max")(6).Text
Worksheets("Weather").Range("I8").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/temperature/@min")(6).Text
Worksheets("Weather").Range("I9").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/clouds/@all")(6).Text
Worksheets("Weather").Range("I10").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/humidity/@value")(6).Text
Worksheets("Weather").Range("I11").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@probability")(6).Text
Worksheets("Weather").Range("I12").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/precipitation/@value")(6).Text
Worksheets("Weather").Range("I13").Value = xmlresponse.SelectNodes("//weatherdata/forecast/time/windSpeed/@mps")(6).Text
'MsgBox (xmlresponse.getElementsByTagName("temperature")(0).Attributes(1).Text) Alternate method to parse XML
End Sub