引入天气形状的Excel VBA - 尝试在重新运行之前删除形状

时间:2014-02-13 21:17:56

标签: excel excel-vba shapes weather vba

我目前正在使用此代码提取5天的预测以及一些适当的图片作为作业。我是根据我发现的视频构建的,但是我遇到了为什么delshape进程没有按照它应该去除形状的问题。

如果有人有任何建议,我会很感激,并试图解释如果可能的错误。我正在努力学习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

1 个答案:

答案 0 :(得分:2)

Shapes.AddPicture从现有文件创建图片。它返回一个表示新图片的Shape对象。您可以在Shapes.AddPicture Method

中详细了解相关信息

更改行

If delshape.Type = msoAutoShape Then delshape.Delete

If delshape.Type = msoPicture Then delshape.Delete