返回列表 上一主題 發帖

[發問] CreateObject("InternetExplorer.Application") 執行完有時會無法跳出??

回復  t8899
一直出現"ok"訊息視窗??  你是中學生應該會關閉這"ok"視窗.有問題試著自己解決,VBA才會進步. ...
GBKEE 發表於 2014-1-14 13:50

我改了一下,是否跟這有關??(紅色部份)
Sub timestock()
Application.ScreenUpdating = False
    Dim i As Integer, S As Integer, K As Integer, j As Integer
    Dim Element
    With CreateObject("InternetExplorer.Application")
        '.Visible = True           '可顯示網頁
       .Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
        Set Element = .document.getelementsbytagname("table")
        With Sheets("SHEET5")
         '  .Cells.Clear
          .Range("A1:F17").ClearContents  
            'For s = 0 To element.Length - 1 '不知此網頁的table內容:請先行測試網頁的完整table內容
            For S = 2 To 3                    '已找出網頁的table內容在 0-3 中
                For i = 0 To Element(S).Rows.Length - 1
                    K = K + 1
               '  For j = 0 To Element(S).Rows(i).Cells.Length - 1   '資料的欄位共6位
                     j=2
                         .Cells(K, j + 1) = Element(S).Rows(i).Cells(j).innerText
                    Next
            '   Next
            Next
     '       .Cells.EntireColumn.AutoFit            '自動調整欄寬
      '      .Cells.EntireRow.AutoFit               '自動調整列高
        End With
       .Quit
    End With
    Set Element = Nothing
  '  MsgBox "0K"
End Sub

TOP

回復  t8899
這方向是對的,有進步
GBKEE 發表於 2014-1-14 15:31

   With CreateObject("InternetExplorer.Application")
           .Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS
  .Quit

請問30秒抓一次資料, 抓完無須 quit ,可以改成每隔30秒再繼續抓嗎??(中間做一個循還)
不用一直執行 iexplorer.exe 開開關關???

TOP

循還的範圍
.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
        Set Element = .document.getelementsbytagname("table")
        With Sheets("SHEET1")
           .Cells.Clear
           K = 0
            'For s = 0 To element.Length - 1 '不知此網頁的table內容:請先行測試網頁的完整table內容
            For S = 2 To 2                    '已找出網頁的table內容在 0-3 中
                For i = 0 To Element(S).Rows.Length - 1
                    K = K + 1
               '  For j = 0 To Element(S).Rows(i).Cells.Length - 1   '資料的欄位共6位
                  j = 2
                         .Cells(K, j + 1) = Element(S).Rows(i).Cells(j).innerText
                        
                '    Next
               Next
            Next
     '       .Cells.EntireColumn.AutoFit            '自動調整欄寬
      '      .Cells.EntireRow.AutoFit               '自動調整列高
        End With

TOP

回復  t8899
ThisWorkbook 程式碼
GBKEE 發表於 2014-1-15 16:31

請問如何停止??

TOP

回復  t8899
ThisWorkbook 程式碼
GBKEE 發表於 2014-1-15 16:31

30秒時間想由K1儲存格來控制,要如何改??

Dim i As Integer, S As Integer, K As Integer, j As Integer
     Dim Element
     Dim RNG
    If Sheet3.Range("K1").Value = 1 Then Set RNG = #12:01:00 AM#
    If Sheet3.Range("K1").Value = 2 Then Set RNG = #12:02:00 AM#
    If Sheet3.Range("K1").Value = 3 Then Set RNG = #12:00:30 AM#
    If Sheet3.Range("K1").Value = 4 Then Set RNG = #12:00:10 AM#

    Application.OnTime Time + RNG, "timestock"

----------------------------------------------------
#12:01:00 AM# 需要物件 ???

TOP

30秒時間想由K1儲存格來控制,要如何改??

Dim i As Integer, S As Integer, K As Integer, j As Inte ...
t8899 發表於 2014-1-15 20:36

抱歉,打擾.......已解決....................
If Sheet3.Range("K1").Value = 1 Then Application.OnTime Time + #12:01:00 AM#, "timestock"
    If Sheet3.Range("K1").Value = 2 Then Application.OnTime Time + #12:02:00 AM#, "timestock"
    If Sheet3.Range("K1").Value = 3 Then Application.OnTime Time + #12:00:30 AM#, "timestock"
    If Sheet3.Range("K1").Value = 4 Then Application.OnTime Time + #12:00:10 AM#, "timestock"

TOP

回復  t8899
此網頁的程式碼
GBKEE 發表於 2014-1-13 17:40


是否少了 Set Element = Nothing 這一行??
應插在34或36行前面??

TOP

本帖最後由 t8899 於 2014-2-10 14:59 編輯
回復  t8899
GBKEE 發表於 2014-1-16 07:25


請問今天連了兩了小時出現錯誤在這一行
Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop


如何解決?
1直接跳出
2.不理會錯誤
此兩種狀況,各如何處理?

TOP

        靜思自在 : 發脾氣是短暫的發瘋。
返回列表 上一主題