返回列表 上一主題 發帖

[發問] 股價VBA的檔案無法更新

本帖最後由 GBKEE 於 2020-4-1 16:29 編輯

回復 4# abc9gad2016
試試看
  1. Sub 更新全部()
  2.     Call 共用參照: If uRow <= 0 Then Exit Sub
  3.     uHead(0, 0) = "※更新中.............."
  4.     uHead(2, 12).Resize(uRow).ClearContents
  5.     For Each uRng In uClmnNo
  6.         uRng(1, 3).Resize(1, 10).ClearContents
  7.         網頁元素_htmlfile uRng
  8.         Beep
  9.     Next
  10.     uHead(0, 0) = "※更新時間:" & Format(Now, "yyyy/mm/dd hh:mm:ss")
  11.     ThisWorkbook.Save
  12. End Sub

  13. Sub 網頁元素_htmlfile(uRng As Range)
  14.     Dim oXmlhttp As Object, oHtmldoc As Object, surl As String, E As Object, i As Integer
  15.     Set oXmlhttp = CreateObject("msxml2.xmlhttp")
  16.     Set oHtmldoc = CreateObject("htmlfile")
  17.    If uRng = "" Then Exit Sub
  18.     surl = "https://tw.stock.yahoo.com/q/q?s=" & uRng
  19.     With oXmlhttp
  20.         .Open "Get", surl, False
  21.         .Send
  22.         oHtmldoc.write .responseText
  23.     End With
  24.     On Error GoTo Ne    '處理股票代碼不存在時程式的出錯
  25.      With oHtmldoc
  26.         Set E = .all.tags("TABLE")(2).Rows(1).Cells  '股票代碼不存時  E Is Nothing
  27.         '** .Rows(1).Cells 網頁表格的內容 ****
  28.         uRng.Cells(1, 2) = Split(E(0).INNERTEXT, vbCrLf)(0)     '去掉換行後的字元
  29.         uRng.Cells(1, 2) = Replace(uRng.Cells(1, 2), uRng, "") '消除股票代碼
  30.         For i = 2 To E.Length - 2
  31.                If i = 2 + 3 Then
  32.                     uRng.Cells(1, i + 1) = Mid(E(i).INNERTEXT, 2) '**消除漲跌的符號**
  33.                 Else
  34.                     uRng.Cells(1, i + 1) = E(i).INNERTEXT
  35.                 End If
  36.         Next
  37.         uRng.Cells(1, i + 1) = E(1).INNERTEXT  '交易時間
  38.     End With
  39. Ne:
  40.   uRng.Interior.Color = IIf(E Is Nothing, vbRed, xlAutomatic) '
  41.     Set oXmlhttp = Nothing  '
  42.     Set oHtmldoc = Nothing
  43. End Sub
複製代碼
1

評分人數

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 人的眼睛長在前面,只看到別人的缺點,絲毫看不到自己的缺點。
返回列表 上一主題