返回列表 上一主題 發帖

上網抓股票資料

回復 5# chairles59
茲將 GBKEE 版大給你的提示整理並稍加修正,
其實 GBKEE 版大已經指引出你的問題所在。
  1. Sub Ex()
  2.     Dim po As Integer                  '  宣告 PO 為整數
  3.    
  4.     lR = Range("A2").End(xlDown).Row      '  R = 9 : Variant/Long
  5.     Rows(lR + 1 & ":400").Clear                    '  直接清除儲存格,不必 Select 再刪除
  6.     '  Selection.Delete shift:=xlUp
  7.    
  8.     LRA = Range("B2").End(xlDown).Row  '  LRA = 9 : Variant/Long

  9.     For i = 3 To LRA
  10.         '  If Cells(i, 3) <> "" Then    ' 工作表 C 欄 ,沒有買進日期,所以不會執行匯入資料的程式碼
  11.         If Cells(i, 3) = "" Then        ' 修正為空值時始進行資料匯入
  12.             Valuesno = "$A$" & i
  13.             Linkss = "URL;https://tw.stock.yahoo.com/q/q?s=" & Cells(i, 1)
  14.         
  15.             po = lR - 5 + (7 * (i - 2)) '  定抓取資料表格迴圈

  16.             With ActiveSheet.QueryTables.Add(Connection:= _
  17.                                 Linkss, Destination:=ActiveSheet.Range("B" & po))
  18.                 .FieldNames = True
  19.                 .RowNumbers = False
  20.                 .FillAdjacentFormulas = False
  21.                 .PreserveFormatting = True
  22.                 .RefreshOnFileOpen = False
  23.                 .BackgroundQuery = True
  24.                 .RefreshStyle = xlInsertDeleteCells
  25.                 .SavePassword = False
  26.                 .SaveData = True
  27.                 .AdjustColumnWidth = True
  28.                 .RefreshPeriod = 0
  29.                 .WebSelectionType = xlSpecifiedTables
  30.                 .WebFormatting = xlWebFormattingNone
  31.                 .WebTables = "6"
  32.                 .WebPreFormattedTextToColumns = True
  33.                 .WebConsecutiveDelimitersAsOne = True
  34.                 .WebSingleBlockTextImport = False
  35.                 .WebDisableDateRecognition = False
  36.                 .WebDisableRedirections = False
  37.                 .Refresh BackgroundQuery:=False
  38.                 .Name = .ResultRange.Cells(3, 1)
  39.             End With
  40.             
  41.             Range("A" & po + 2) = Cells(i, 1)
  42.             '  Cells(i, 2) = "=vlookup(" & Cells(i, 1) & ",$A$16:$O$200,2,0)"    '  是英文字母 O,不是數字 0
  43.             Cells(i, 2) = "=vlookup(" & Cells(i, 1) & ",$A$11:$O$200,2,0)"       '  從 $A$16 起始之範圍會造成 B3 得出不正確值
  44.             Cells(i, 2) = Mid(Cells(i, 2).Text, 5)                               '  Mid(Cells(i, 2), 5) 會產生執行階段錯誤 13 型態不符
  45.             
  46.             '  Cells(i, 7) = "=vlookup(" & Cells(i, 1) & ",$A$16:$O$200,4,0)"    '  是英文字母 O,不是數字 0
  47.             Cells(i, 7) = "=vlookup(" & Cells(i, 1) & ",$A$11:$O$200,4,0)"       '  從 $A$16 起始之範圍會造成 G3 得出不正確值
  48.         End If
  49.     Next
  50. End Sub
複製代碼

TOP

回復 5# chairles59

TOP

        靜思自在 : 有智慧才能分辨善惡邪正;有謙虛才能建立美滿人生。
返回列表 上一主題