带有天气形状的Excel VBA – 尝试在重新运行之前删除形状

我目前正在使用这个代码来提取5天的预测,以及一些体面的图片作业。 我已经把它制作成了一个我发现的video,但是我为什么desshape过程没有按照它应该去除的形状而遇到麻烦。

如果有人有任何build议,我将不胜感激,并试图解释什么是错的,如果可能的话。 我正在尝试尽可能地学习VBA,因为我是一个全新的用户。

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从现有的文件创build一个图片。 它返回一个表示新图片的Shape对象。 你可以在Shapes.AddPicture方法中阅读更多

改变线

 If delshape.Type = msoAutoShape Then delshape.Delete 

 If delshape.Type = msoPicture Then delshape.Delete 
Interesting Posts