Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 8788|回复: 10

[已解决]如何 用VBA 获取 互联网日期

[复制链接]
发表于 2013-7-17 14:43 | 显示全部楼层 |阅读模式
如何 用VBA 获取 互联网日期 并返回日期值
不是从本电脑中用DATE获取,而是获取互联网上的日期。谢谢
最佳答案
2013-7-17 14:56

  1.   Sub getdate()
  2.     On Error Resume Next
  3.     Dim objXML As Object
  4.     Dim strTemp As String
  5.     Dim lStart As Long
  6.     Dim lEnd As Long
  7.     Dim DtWeb As Date
  8.     Dim arr() As String
  9.    
  10.     DtWeb = #12/31/2010#                                                '设置默认返回时间
  11.     '建立XMLHTTP对象。并获取<A href="http://www.timeanddate.com/worldclock/city.html?n=33">http://www.timeanddate.com/worldclock/city.html?n=33</A>的网页Text
  12.     '&Refresh=" & Rnd 是为了避免直接从IE缓存中读取
  13.     Set objXML = CreateObject("Microsoft.XMLHTTP")
  14.     Randomize   '初始化随机数,避免IE缓存重复
  15.     objXML.Open "Get", "<A href="http://www.timeanddate.com/worldclock/city.html?n=33&Refresh">http://www.timeanddate.com/worldclock/city.html?n=33&Refresh</A>=" & Rnd, False   '读取北京时间
  16.     objXML.sEnd ""
  17.     strTemp = objXML.responseText</P>
  18.     '对网页进行处理,找出当前日期和时间
  19.     lStart = InStr(1, strTemp, "Current Time", vbTextCompare)
  20.     lEnd = InStr(lStart, strTemp, "</strong>", vbTextCompare)
  21.     strTemp = Mid(strTemp, lStart, lEnd - lStart)</P>
  22.     strTemp = Replace(strTemp, "Current Time</th><td><strong id=ct  class=big>", "")
  23.    
  24.     arr() = Split(strTemp, ",", -1, vbTextCompare)
  25.     DtWeb = CDate(arr(1) & arr(2))
  26.     Set objXML = Nothing
  27.     '活动单元格输出日期和时间
  28.     ActiveCell.Value = DtWeb
  29. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2013-7-17 14:51 | 显示全部楼层
回复

使用道具 举报

发表于 2013-7-17 14:56 | 显示全部楼层    本楼为最佳答案   

  1.   Sub getdate()
  2.     On Error Resume Next
  3.     Dim objXML As Object
  4.     Dim strTemp As String
  5.     Dim lStart As Long
  6.     Dim lEnd As Long
  7.     Dim DtWeb As Date
  8.     Dim arr() As String
  9.    
  10.     DtWeb = #12/31/2010#                                                '设置默认返回时间
  11.     '建立XMLHTTP对象。并获取<A href="http://www.timeanddate.com/worldclock/city.html?n=33">http://www.timeanddate.com/worldclock/city.html?n=33</A>的网页Text
  12.     '&Refresh=" & Rnd 是为了避免直接从IE缓存中读取
  13.     Set objXML = CreateObject("Microsoft.XMLHTTP")
  14.     Randomize   '初始化随机数,避免IE缓存重复
  15.     objXML.Open "Get", "<A href="http://www.timeanddate.com/worldclock/city.html?n=33&Refresh">http://www.timeanddate.com/worldclock/city.html?n=33&Refresh</A>=" & Rnd, False   '读取北京时间
  16.     objXML.sEnd ""
  17.     strTemp = objXML.responseText</P>
  18.     '对网页进行处理,找出当前日期和时间
  19.     lStart = InStr(1, strTemp, "Current Time", vbTextCompare)
  20.     lEnd = InStr(lStart, strTemp, "</strong>", vbTextCompare)
  21.     strTemp = Mid(strTemp, lStart, lEnd - lStart)</P>
  22.     strTemp = Replace(strTemp, "Current Time</th><td><strong id=ct  class=big>", "")
  23.    
  24.     arr() = Split(strTemp, ",", -1, vbTextCompare)
  25.     DtWeb = CDate(arr(1) & arr(2))
  26.     Set objXML = Nothing
  27.     '活动单元格输出日期和时间
  28.     ActiveCell.Value = DtWeb
  29. End Sub
复制代码
回复

使用道具 举报

发表于 2013-7-17 15:07 | 显示全部楼层
直接和时间服务器做同步吧。
回复

使用道具 举报

 楼主| 发表于 2013-7-17 16:27 | 显示全部楼层
本帖最后由 事后诸葛亮 于 2013-7-17 16:32 编辑
wayy 发表于 2013-7-17 14:56

好详细,还有代码注释,谢谢
不过 出错了。郁闷
刚把代码放入VBE窗体 就显示红色。
QQ截图20130717163026.jpg
运行时,报错。
QQ截图20130717163053.jpg
回复

使用道具 举报

 楼主| 发表于 2013-7-17 16:27 | 显示全部楼层
hwc2ycy 发表于 2013-7-17 15:07
直接和时间服务器做同步吧。

谢谢,你回答我的问题总是那么的积极。哈哈。
回复

使用道具 举报

发表于 2013-7-17 16:37 | 显示全部楼层
本帖最后由 wayy 于 2013-7-17 16:41 编辑
事后诸葛亮 发表于 2013-7-17 16:27
好详细,还有代码注释,谢谢
不过 出错了。郁闷
刚把代码放入VBE窗体 就显示红色。

还是直接贴代码吧{:1012:}

Sub getdate()
    On Error Resume Next
    Dim objXML As Object
    Dim strTemp As String
    Dim lStart As Long
    Dim lEnd As Long
    Dim DtWeb As Date
    Dim arr() As String
   
    DtWeb = #12/31/2010#                                                '设置默认返回时间
    '建立XMLHTTP对象。并获取<A href="http://www.timeanddate.com/worldclock/city.html?n=33">[url]http://www.timeanddate.com/worldclock/city.html?n=33</A[/url]>的网页Text
    '&Refresh=" & Rnd 是为了避免直接从IE缓存中读取
    Set objXML = CreateObject("Microsoft.XMLHTTP")
    Randomize   '初始化随机数,避免IE缓存重复
    objXML.Open "Get", "http://www.timeanddate.com/worldclock/city.html?n=33&Refresh=" & Rnd, False   '读取北京时间
    objXML.sEnd ""
    strTemp = objXML.responseText
    '对网页进行处理,找出当前日期和时间
    lStart = InStr(1, strTemp, "Current Time", vbTextCompare)
    lEnd = InStr(lStart, strTemp, "</strong>", vbTextCompare)
    strTemp = Mid(strTemp, lStart, lEnd - lStart)
    strTemp = Replace(strTemp, "Current Time</th><td><strong id=ct  class=big>", "")
   
    arr() = Split(strTemp, ",", -1, vbTextCompare)
    DtWeb = CDate(arr(1) & arr(2))
    Set objXML = Nothing
    '活动单元格输出日期和时间
    ActiveCell.Value = DtWeb
End Sub

回复

使用道具 举报

发表于 2013-7-17 16:47 | 显示全部楼层
这个的日期要短点
With CreateObject("InternetExplorer.Application")
    .Visible = 0
    .Navigate "http://www.nongli114.com/"
    Do Until .Readystate = 4
       DoEvents
    Loop
    MsgBox "今天是" & .document.all.tags("b")(0).outertext
    .Quit
End With
回复

使用道具 举报

 楼主| 发表于 2013-7-17 17:08 | 显示全部楼层
上清宫主 发表于 2013-7-17 16:47
这个的日期要短点
With CreateObject("InternetExplorer.Application")
    .Visible = 0

人才呀。谢谢。
不过 个人心里感觉,nongli114 有可能会改版之类的,这代码就会失效。
3楼的 那个网址 改版的可能性要小多了。呵呵。
而且 运行你的代码的时候 感觉好慢(卡)。
回复

使用道具 举报

发表于 2017-8-9 12:42 | 显示全部楼层

你好,请问你这个代码是放到: 对象-thiswookbook --open,中吗?
开头和结尾是这样吗?Private Sub Workbook_Open()Sub getdate()
....................
....................
...................
ActiveCell.Value = DtWeb
End Sub


但是关闭重新打开并没有提示时间?
能帮我解决下吗 谢谢
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-19 10:30 , Processed in 0.310180 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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