2017-09-28 36 views
2

在成功從中檢索數據後關閉應用程序後,它會遞歸打開並且無法關閉它。請澄清爲什麼會發生這種情況。需要進行澄清以避免VBA中的遞歸

Sub macro() 
    Dim appIE As Object 
    Dim objElement As Object 
    Dim Y As Long 
    Dim obj As Object 
    Dim r As Long, c As Long, t As Long 
    Dim sathish As String 

    Dim objCollection As Object 
    Dim eRow As Long 

    ' Create InternetExplorer Object 
    Set appIE = CreateObject("InternetExplorer.Application") 

    With appIE 
     appIE.Visible = False 
     appIE.navigate "website" 

     Do While appIE.Busy Or appIE.ReadyState <> 4 
      DoEvents 
     Loop 

     appIE.Visible = True 
     appIE.document.getElementById("Username").Value = "" 
     appIE.document.getElementById("Password").Value = "" 
     appIE.document.getElementById("btnSubmit").Click 

     Number = Range("A1", Range("A1").End(xlDown)).Rows.Count 
     Number = 10 

     For Y = 1 To Number 
      sathish = Cells(Y, 1).Value 
      appIE.document.getElementById("__tab_tabFilter").Click 
      appIE.document.getElementById("filter_ReferenceCode").Value = sathish 
      appIE.document.getElementById("filter_btnSetFilter").Click 
      'Set IE = Nothing 
      Set objCollection = appIE.document.getElementsByTagName("TABLE") 

      For t = 10 To (objCollection.Length - 1) 
       For r = 0 To (objCollection(t).Rows.Length - 1) 
        eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 

        For c = 0 To (objCollection(t).Rows(r).Cells.Length - 1) 
         ThisWorkbook.Worksheets(2).Cells(eRow, c + 1) = objCollection(t).Rows(r).Cells(c).innerText 
        Next c 
       Next r 

       If t = objCollection.Length - 1 Then 
        Cells(eRow + 1, 1) = "" 
       End If 
      Next t 
     Next Y 
    End With 
    ' Set IE = Nothing 
End Sub 
+0

你是否有任何機會表示再次調用子/函數或者VBA本身會重新打開? – Paramone

+0

是的,VBA本身會重新打開 –

+0

也許您有其他代碼響應單元格更新時?例如。工作表更改事件。嘗試在模塊的頂部放置'Application.EnableEvents = False',然後在末尾使用'Application.EnableEvents = True'。這將在這個Sub運行時禁用事件。 –

回答

0

我想知道......文檔上的'點擊'可能是在新的瀏覽器窗口/選項卡中打開文檔?