返回列表 上一主題 發帖

[發問] 請問這可設定等幾秒沒開網頁或網頁錯誤就執行下一筆嗎,謝謝

回復 16# GBKEE
請教 G 大大
我改了一些資料 有些資料還收不到
請 G 大 看那裡要改的
謝謝
  1. Dim ie As Object   '模組最頂端 Dim 供這模組的程序使用的變數
  2. Sub AllFile()
  3.     Dim i As Integer, v, Y As Integer, S As String
  4.     Set ie = CreateObject("internetexplorer.application")   '使用此方式可以免除 "設定引用項目"
  5.     With ie '縮小IE視窗
  6.         .Visible = True
  7.         .Width = 5
  8.         .Height = 5
  9.     End With
  10.     With 工作表1
  11.       Dim AR
  12.         AR = .Range("E1:G1")
  13.         .Range("E:G") = ""
  14.         .Range("E1:G1") = AR

  15. '        .Range("E2").CurrentRegion = ""            '清除工作表1,年度範圍
  16.         For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
  17.             v = .Cells(i, 1).Value
  18.             GetDividend (v)
  19.           .Cells(i, 5).Value = 工作表2.Cells(2, 2).Value
  20.            .Cells(i, 6).Value = 工作表2.Cells(2, 5).Value
  21.            GetClosePrice (v)
  22.            .Cells(i, 7).Value = 工作表3.Cells(2, 8).Value
  23.             
  24.         Next
  25.     End With
  26.     With ie  'IE視窗最大化
  27.         Application.WindowState = xlMaximized
  28.         .Height = Application.Height
  29.         .Width = Application.Width
  30.         .Quit
  31.     End With
  32. End Sub
  33. 'Private Sub GetDividend(ByVal ss As String)


  34. Private Sub GetDividend(ByVal ss As String)     '取股利網頁

  35.     Dim rr As String, T As Date, i, ii, k, j, S As Object
  36.     'On Error Resume Next '程式的執行沒有預期的錯誤,此行可不用.
  37.     T = Time
  38.     rr = "http://dj.mybank.com.tw/z/zc/zcc/zcc_" & ss & ".asp.htm"
  39.     With ie
  40.         .Navigate rr
  41.         Do While .readyState <> 4                          '等待網頁下載完畢
  42.               DoEvents
  43.               If Time >= T + #12:00:03 AM# Then            '等待8秒 3秒太少會誤錯改8妙
  44.                 DoEvents
  45.                 Application.SendKeys "~"                    '股票代號錯誤,網頁會有訊息,須按確定,才可繼續下面股票代號
  46.                
  47.                 Exit Do
  48.               End If
  49.         Loop
  50.         ''***不是等待8秒 3秒太少會誤錯改8妙 ***
  51.         Do
  52.         Set S = .Document.getElementsByTagName("table")(3) ' 新的 table 4
  53.         Loop Until Not S Is Nothing
  54.         '*** 勝一 沒捉到 ????
  55.         '*** 因程式運行太快 , S Is Nothing, S 尚未指定為.Document.getElementsByTagName("table")(4)
  56.         '*** 程式已經執行下一行, With 工作表2 的程式碼

  57.         With 工作表2
  58.             .UsedRange.Clear
  59.             For i = 0 To S.Rows.Length - 1      '寫入資料
  60.                 k = k + 1
  61.                 'For j = 0 To S.Rows(i).Length - 1  '這行是錯誤的 也是多餘的迴圈
  62.                    '用 On Error Resume Next 使程式繼續執行
  63.                     For ii = 0 To S.Rows(i).Cells.Length - 1  ' S.Rows(i).Cells.Length - 1 才是正確
  64.                     .Cells(k, ii + 1) = S.Rows(i).Cells(ii).innerText
  65.                     DoEvents
  66.                     Next
  67.                 'Next
  68.             Next
  69.         End With
  70.     End With
  71. End Sub

  72. Private Sub GetClosePrice(ByVal ss As String) ' 取收盤價網頁
  73.     Dim rr As String, T As Date, i, ii, k, j, S As Object
  74.     'On Error Resume Next '程式的執行沒有預期的錯誤,此行可不用.
  75.     T = Time
  76.     rr = "https://djinfo.cathaysec.com.tw/Z/ZC/ZCX/ZCXNEWCATHAYSEC.DJHTM?A=" & ss
  77.     With ie
  78.         .Navigate rr
  79.         Do While .readyState <> 4                          '等待網頁下載完畢
  80.               DoEvents
  81.               If Time >= T + #12:00:03 AM# Then            '等待8秒 3秒太少會誤錯改8妙
  82.                 DoEvents
  83.                 Application.SendKeys "~"                    '股票代號錯誤,網頁會有訊息,須按確定,才可繼續下面股票代號
  84.                
  85.                 Exit Do
  86.               End If
  87.         Loop
  88.         ''***不是等待8秒 3秒太少會誤錯改8妙 ***
  89.         Do
  90.         Set S = .Document.getElementsByTagName("table")(2) ' 新的 table 4
  91.         Loop Until Not S Is Nothing
  92.         '*** 勝一 沒捉到 ????
  93.         '*** 因程式運行太快 , S Is Nothing, S 尚未指定為.Document.getElementsByTagName("table")(4)
  94.         '*** 程式已經執行下一行, With 工作表2 的程式碼

  95.         With 工作表3
  96.             .UsedRange.Clear
  97.             For i = 0 To S.Rows.Length - 1      '寫入資料
  98.                 k = k + 1
  99.                 'For j = 0 To S.Rows(i).Length - 1  '這行是錯誤的 也是多餘的迴圈
  100.                    '用 On Error Resume Next 使程式繼續執行
  101.                     For ii = 0 To S.Rows(i).Cells.Length - 1  ' S.Rows(i).Cells.Length - 1 才是正確
  102.                     .Cells(k, ii + 1) = S.Rows(i).Cells(ii).innerText
  103.                     DoEvents
  104.                     Next
  105.                 'Next
  106.             Next
  107.         End With
  108.     End With
  109. End Sub
複製代碼

配息測試-3.rar (29.12 KB)

TOP

回復 19# joey0415
謝謝 大大
國泰的資料真的快多了

TOP

回復 18# wufonna

其實就是錄製好後,把某行註解後,再執行,如果可以就表示該行不需行,因為錄製的通常會有很多不要的代碼
錄久了就知道…
最後加上.delete
表示取消連線
  1. Sub 巨集1()
  2.     With ActiveSheet.QueryTables.Add(Connection:= _
  3.         "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCX/ZCXNEWCATHAYSEC.DJHTM?A=2882", _
  4.         Destination:=Range("$A$1"))
  5.         .WebFormatting = xlWebFormattingNone
  6.         .WebTables = "2,3"
  7.         .Refresh BackgroundQuery:=False
  8.         .Delete
  9.     End With
  10. End Sub
複製代碼

TOP

本帖最後由 wufonna 於 2014-9-3 21:11 編輯

回復 15# joey0415
  1. Sub 巨集1()
  2. '
  3. ' 巨集1 巨集
  4. '

  5. '
  6.     With ActiveSheet.QueryTables.Add(Connection:= _
  7.         "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCX/ZCXNEWCATHAYSEC.DJHTM?A=2882", _
  8.         Destination:=Range("$A$1"))
  9.         .Name = "ZCXNEWCATHAYSEC.DJHTM?A=2882"
  10.         .FieldNames = True
  11.         .RowNumbers = False
  12.         .FillAdjacentFormulas = False
  13.         .PreserveFormatting = True
  14.         .RefreshOnFileOpen = False
  15.         .BackgroundQuery = True
  16.         .RefreshStyle = xlInsertDeleteCells
  17.         .SavePassword = False
  18.         .SaveData = True
  19.         .AdjustColumnWidth = True
  20.         .RefreshPeriod = 0
  21.         .WebSelectionType = xlSpecifiedTables
  22.         .WebFormatting = xlWebFormattingNone
  23.         .WebTables = "2,3"
  24.         .WebPreFormattedTextToColumns = True
  25.         .WebConsecutiveDelimitersAsOne = True
  26.         .WebSingleBlockTextImport = False
  27.         .WebDisableDateRecognition = False
  28.         .WebDisableRedirections = False
  29.         .Refresh BackgroundQuery:=False
  30.     End With
  31. End Sub
複製代碼
請教 大大 這段如何精簡程式碼
像 table 這樣
因要從中取得收盤價
謝謝 大大

TOP

回復 16# GBKEE


    謝謝 G 大大
    程式運行順暢 ^_^
   
本想二維的表單怎會有三個 FOR NEXT
正想發問

再請在 G 大大
新手如何去發現程式中沒有錯誤的變化
像程式中的
Do
        Set S = .Document.getElementsByTagName("table")(4) ' 新的 table 4
        Loop Until Not S Is Nothing
謝謝

TOP

回復 14# wufonna
請詳看註解
  1. Private Sub GetDividend(ByVal ss As String)
  2.     Dim rr As String, T As Date, i, ii, k, j, S As Object
  3.     'On Error Resume Next '程式的執行沒有預期的錯誤,此行可不用.
  4.     T = Time
  5.     rr = "http://yamstock.megatime.com.tw/asp/stockinfo/GetReport.asp?select_table=html\Ficxxx\&stockid=" & ss & "&name1=D4&index1=12"     ' 正確的
  6.     With ie
  7.         .Navigate rr
  8.         Do While .readyState <> 4                          '等待網頁下載完畢
  9.               DoEvents
  10.               If Time >= T + #12:00:03 AM# Then            '等待8秒 3秒太少會誤錯改8妙
  11.                 DoEvents
  12.                 Application.SendKeys "~"                    '股票代號錯誤,網頁會有訊息,須按確定,才可繼續下面股票代號
  13.                
  14.                 Exit Do
  15.               End If
  16.         Loop
  17.         ''***不是等待8秒 3秒太少會誤錯改8妙 ***
  18.         Do
  19.         Set S = .Document.getElementsByTagName("table")(4) ' 新的 table 4
  20.         Loop Until Not S Is Nothing
  21.         '*** 勝一 沒捉到 ????
  22.         '*** 因程式運行太快 , S Is Nothing, S 尚未指定為.Document.getElementsByTagName("table")(4)
  23.         '*** 程式已經執行下一行, With 工作表2 的程式碼

  24.         With 工作表2
  25.             .UsedRange.Clear
  26.             For i = 0 To S.Rows.Length - 1      '寫入資料
  27.                 k = k + 1
  28.                 'For j = 0 To S.Rows(i).Length - 1  '這行是錯誤的 也是多餘的迴圈
  29.                    '用 On Error Resume Next 使程式繼續執行
  30.                     For ii = 0 To S.Rows(i).Cells.Length - 1  ' S.Rows(i).Cells.Length - 1 才是正確
  31.                     .Cells(k, ii + 1) = S.Rows(i).Cells(ii).innerText
  32.                     DoEvents
  33.                     Next
  34.                 'Next
  35.             Next
  36.         End With
  37.     End With
  38. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 joey0415 於 2014-9-3 11:19 編輯

回復 14# wufonna

https://djinfo.cathaysec.com.tw/Z/ZC/ZCC/ZCC.DJHTM?A=1101

代碼變數自己改改就行
  1. Sub 巨集1()
  2. '
  3.     With ActiveSheet.QueryTables.Add(Connection:= _
  4.         "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCC/ZCC.DJHTM?A=1101", Destination _
  5.         :=Range("$A$1"))
  6.         .WebFormatting = xlWebFormattingNone
  7.         .WebTables = "3"
  8.         .Refresh BackgroundQuery:=False
  9.         .Delete
  10.     End With
  11. End Sub
複製代碼
用這種網站抓資料比較用,不用等幾秒...

網址又清楚

yam會下載一堆沒用的東西

把所有抓下來排序一下,就知道有哪些股是沒有抓完整的,再抓一次就可以了…

通常下載都是會找穩定、速度快,純資料為最佳

參考

TOP

回復 13# GBKEE

GBKEE 大大
程式沒有錯誤
可是有時資料沒捉到
像這次 勝一 沒捉到
謝謝

2014-09-02_171247.jpg (49.77 KB)

2014-09-02_171247.jpg

配息測試-2.rar (21.67 KB)

TOP

回復 12# wufonna
11#所說的錯誤,程式碼在哪發生的.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 11# wufonna


    請問 G 大大 是
   If Time >= T + #12:00:08 AM# Then        
等代時間少的問題嗎 我將3秒改8秒就沒錯了
如果股票多了如何解決 謝謝

TOP

        靜思自在 : 自己害自己,莫過於亂發脾氣。
返回列表 上一主題