|
|
- Sub 获取天气()
- Dim shp As Shape, URL$, str1$, str2$, str3$, today$
- Dim i As Integer, xm As Variant, rq As Date, xq As String
- Dim nf As Integer ' 当前年份
- Application.ScreenUpdating = False
- Cells.UnMerge
- Range("D2:K" & Rows.Count).ClearContents
- For Each shp In ActiveSheet.Shapes
- If shp.Type <> 1 Then shp.Delete
- Next
- URL = "http://www.15tianqi.com/" & getpy(Range("B2").Value) & "/"
- With CreateObject("MSXML2.XMLHTTP")
- .Open "GET", URL, False
- .setRequestHeader "Connection", "keep-alive"
- .send
- str1 = .responsetext
- str3 = "<table>" & Split(Split(str1, Range("B2").Value & "今日天气预报</dt>")(1), "</td>")(0) & "</td></table>"
- str2 = "<table" & Split(Split(str1, "<table")(3), "</table>")(0) & "</table>"
- str2 = Replace(str2, "/Images", "http://www.15tianqi.com/Images")
- str3 = Replace(str3, "/Images", "http://www.15tianqi.com/Images")
- ' 调整str2从当天开始
- today = Format(Date, "M") & "月" & Format(Date, "d") & "日"
- nf = Year(Date) ' 当前年份
- Dim pos As Long: pos = InStr(1, str2, today)
- If pos > 0 Then
- Dim trStart As Long: trStart = InStrRev(str2, "<tr>", pos)
- If trStart > 0 Then
- Dim headerEnd As Long: headerEnd = InStr(1, str2, "</tr>") + 5
- If headerEnd > 5 Then
- str2 = Left(str2, headerEnd) & Mid(str2, trStart)
- End If
- End If
- End If
- CopyToClipbox str2
- ActiveSheet.Paste Destination:=Range("D2")
- CopyToClipbox str3
- ActiveSheet.Paste Destination:=Range("K3")
- Range("K1") = Range("B2") & "今日天气"
- Range("D1") = "★" & Range("B2") & "未来15天天气预报★"
- ' 修正15天预报表格中的星期(从D3开始的日期行)
- For i = 3 To 17
- If Trim(Cells(i, 4).Value) = "" Then Exit For
- xm = Split(Trim(Cells(i, 4).Value))
- If UBound(xm) >= 0 Then
- Dim monthStr As String: monthStr = Replace(xm(0), "月", "")
- monthStr = Split(monthStr, "日")(0)
- Dim targetDay As Integer: targetDay = Val(Split(xm(0), "日")(0))
- On Error Resume Next
- rq = CDate(nf & "年" & xm(0))
- If Err.Number <> 0 Then
- Err.Clear
- rq = CDate(nf + 1 & "年" & xm(0)) ' 跨年时自动使用下一年
- End If
- On Error GoTo 0
- xq = Format(rq, "aaaa")
- Cells(i, 4).Value = xm(0) & " " & xq
- End If
- Next i
- End With
- Range("B5").Select
- Application.ScreenUpdating = True
- End Sub
- Sub CopyToClipbox(strText As String)
- With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
- .SetText strText
- .PutInClipboard
- End With
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|