ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
Python自动化办公应用大全 Excel 2021函数公式学习大典 Kutools for Office 套件发布 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: 87009997

[求助] 求修改代码,谢谢

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-12-17 08:31 | 显示全部楼层
本帖最后由 ykcbf1100 于 2025-12-17 08:56 编辑
taller 发表于 2025-12-16 23:35
To 草爷:

这部分代码有个小bug,第一行代码将”星期日“替换为”星期一“,最后一行再次被替换为”星 ...

谢谢指正,已修复星期显示问题

QQ_1765932510199.png

网抓获取天气预报NO2.zip

60.54 KB, 下载次数: 5

TA的精华主题

TA的得分主题

发表于 2025-12-17 08:55 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
87009997 发表于 2025-12-17 00:19
数据没有按要求提取到sheet1表格内

我看错题意了,我只提取天气预报
反正有别人写了代码,我就不改了。

TA的精华主题

TA的得分主题

发表于 2025-12-17 08:57 | 显示全部楼层
  1. Sub 获取天气()
  2.     Dim shp As Shape, URL$, str1$, str2$, str3$, today$
  3.     Dim i As Integer, xm As Variant, rq As Date, xq As String
  4.     Dim nf As Integer  ' 当前年份
  5.     Application.ScreenUpdating = False
  6.     Cells.UnMerge
  7.     Range("D2:K" & Rows.Count).ClearContents
  8.     For Each shp In ActiveSheet.Shapes
  9.         If shp.Type <> 1 Then shp.Delete
  10.     Next
  11.     URL = "http://www.15tianqi.com/" & getpy(Range("B2").Value) & "/"
  12.     With CreateObject("MSXML2.XMLHTTP")
  13.         .Open "GET", URL, False
  14.         .setRequestHeader "Connection", "keep-alive"
  15.         .send
  16.         str1 = .responsetext
  17.         str3 = "<table>" & Split(Split(str1, Range("B2").Value & "今日天气预报</dt>")(1), "</td>")(0) & "</td></table>"
  18.         str2 = "<table" & Split(Split(str1, "<table")(3), "</table>")(0) & "</table>"
  19.         str2 = Replace(str2, "/Images", "http://www.15tianqi.com/Images")
  20.         str3 = Replace(str3, "/Images", "http://www.15tianqi.com/Images")
  21.         ' 调整str2从当天开始
  22.         today = Format(Date, "M") & "月" & Format(Date, "d") & "日"
  23.         nf = Year(Date)  ' 当前年份
  24.         Dim pos As Long: pos = InStr(1, str2, today)
  25.         If pos > 0 Then
  26.             Dim trStart As Long: trStart = InStrRev(str2, "<tr>", pos)
  27.             If trStart > 0 Then
  28.                 Dim headerEnd As Long: headerEnd = InStr(1, str2, "</tr>") + 5
  29.                 If headerEnd > 5 Then
  30.                     str2 = Left(str2, headerEnd) & Mid(str2, trStart)
  31.                 End If
  32.             End If
  33.         End If
  34.         CopyToClipbox str2
  35.         ActiveSheet.Paste Destination:=Range("D2")
  36.         CopyToClipbox str3
  37.         ActiveSheet.Paste Destination:=Range("K3")
  38.         Range("K1") = Range("B2") & "今日天气"
  39.         Range("D1") = "★" & Range("B2") & "未来15天天气预报★"
  40.         ' 修正15天预报表格中的星期(从D3开始的日期行)
  41.         For i = 3 To 17
  42.             If Trim(Cells(i, 4).Value) = "" Then Exit For
  43.             xm = Split(Trim(Cells(i, 4).Value))
  44.             If UBound(xm) >= 0 Then
  45.                 Dim monthStr As String: monthStr = Replace(xm(0), "月", "")
  46.                 monthStr = Split(monthStr, "日")(0)
  47.                 Dim targetDay As Integer: targetDay = Val(Split(xm(0), "日")(0))
  48.                 On Error Resume Next
  49.                 rq = CDate(nf & "年" & xm(0))
  50.                 If Err.Number <> 0 Then
  51.                     Err.Clear
  52.                     rq = CDate(nf + 1 & "年" & xm(0))  ' 跨年时自动使用下一年
  53.                 End If
  54.                 On Error GoTo 0
  55.                 xq = Format(rq, "aaaa")
  56.                 Cells(i, 4).Value = xm(0) & " " & xq
  57.             End If
  58.         Next i
  59.     End With
  60.     Range("B5").Select
  61.     Application.ScreenUpdating = True
  62. End Sub

  63. Sub CopyToClipbox(strText As String)
  64.     With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
  65.         .SetText strText
  66.         .PutInClipboard
  67.     End With
  68. End Sub
复制代码


评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-12-17 18:59 , Processed in 0.018882 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表