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

标签 excel 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

最佳答案

Shapes.AddPicture 从现有文件创建图片。它返回一个代表新图片的 Shape 对象。您可以在Shapes.AddPicture Method中阅读更多相关信息。

换行

If delshape.Type = msoAutoShape Then delshape.Delete

If delshape.Type = msoPicture Then delshape.Delete

关于引入天气形状的 Excel VBA - 尝试在重新运行之前删除形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765676/

相关文章:

javascript - 使用javascript将大型html表格导出到excel

arrays - numpy ndarray 形状有什么作用?

javascript - [HTML][Javascript][Canvas] 无法使多边形函数工作

wpf - Path中加号的Data值是多少

c# - 通过提供邮政编码获取位置名称

vba - 仅将颜色从一个单元格自动复制到另一个单元格 - EXCEL VBA

excel - 如何防止因 DPI 变化而缩放?

Excel:根据每个单元格自身的值对每个单元格进行条件格式设置

python - 如何通过weather api获取降水量/降雨量?

delphi - win32 APP 中的气象信息