我目前正在使用此代码来提取 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/