I am currently using this code to pull out the 5-day forecast along with some decent pictures for an assignment. I had built it off of a video I found but I’m having trouble with why the delshape process isn’t removing the shapes as it should.
If anyone has any recommendations I would appreciate it as well as trying to explain what is wrong if possible. I am trying to learn as much as I can with VBA as I am a brand new user.
Sub CurrentFiveDayForecast() Dim WS As Worksheet: Set WS = ActiveSheet >WS.Range("thedate").Value = "" WS.Range("hightemp").Value = "" WS.Range("lowtemp").Value = "" Dim delshape As Shape For Each delshape In WS.Shapes If delshape.Type = msoAutoShape Then delshape.Delete Next delshape Dim Req As New XMLHTTP Req.Open "GET", "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Hong+Kong&format=xml&num_of_days=5&key=APIKEY", False Req.send Dim Resp As New DomDocument Resp.LoadXML Req.responseText Dim Weather As IXMLDOMNode Dim i As Integer Dim wShape As Shape Dim thiscell As Range For Each Weather In Resp.getElementsByTagName("weather") i = i + 1 WS.Range("thedate").Cells(1, i).Value = Weather.SelectNodes("date")(0).Text WS.Range("hightemp").Cells(1, i).Value = Weather.SelectNodes("tempMaxF")(0).Text WS.Range("lowtemp").Cells(1, i).Value = Weather.SelectNodes("tempMinF")(0).Text Set thiscell = WS.Range("weatherpictures").Cells(1, i) Set wShape = WS.Shapes.AddPicture(Weather.SelectNodes("weatherIconUrl")(0).Text, msoFalse, msoCTrue, thiscell.Left, thiscell.Top, thiscell.Width, thiscell.Height) Next Weather End Sub
Shapes.AddPicture Creates a picture from an existing file. It returns a Shape object that represents the new picture. You can read more about it in Shapes.AddPicture Method
Change the line
If delshape.Type = msoAutoShape Then delshape.Delete
If delshape.Type = msoPicture Then delshape.Delete