2017-02-21 6 views
1

私は奇妙な問題に悩まされているようです。私は以下のExcel VBAコードをWebサイトに訪問し、userID(シート1のuserID列Aから)を入力し、送信ボタンを押した後に表示されるユーザーの名前を取得し、残りのuserIDを続けます。Excel VBA複数ループが動作しない

Public Sub TEST() 
TestPage = "http://test.com/" 

Dim IE As New InternetExplorer 
Dim Doc As HTMLDocument 
Dim GetElem As Object 
Dim GetElem2 As Object 

IE.Visible = True 
IE.navigate TestPage 
Do 
    DoEvents 
Loop Until IE.readyState = READYSTATE_COMPLETE 
Application.Wait (Now + TimeValue("0:00:04")) 

Set Doc = IE.document 
CurRow = Sheet1.UsedRange.Rows.Count 

Do While CurRow > 0 
    Application.Wait (Now + TimeValue("0:00:4")) 

    'Find/Get the userID textbox and enter the current userID 
    For Each GetElem In Doc.getElementsByTagName("input") 
     If GetElem.ID = "query" Then 'I could just do getElementByID later 
      GetElem.Value = Sheet1.Range("A" & CurRow).Value 
     End If 
    Next 

    'Find and click the submit button   
    For Each GetElem2 In Doc.getElementsByTagName("button") 
     If GetElem2.Type = "submit" Then 
      GetElem2.Click 
      Application.Wait (Now + TimeValue("0:00:03")) 
     End If 
    Next 
    CurRow = CurRow - 1 
Loop 
End Sub 

問題はコードが一度しか動作しないことです。テキストボックスに最初のuserIDを入力し、Submitを押します。それがループし、次のuserIDを入力しようとすると、コードはループでスタックします。

2番目のFor-Nextループ全体を削除すると、それは機能します(送信しませんが、テキストボックスの各ユーザーIDが入力されます)。

さらに、コードをステップバイステップでデバッグする場合、すべて正常に動作します。完全にコードを実行している場合にのみ問題を取得。:(

+0

は 'Loop'前に'のDebug.Print CurRow'を入れて、それがすぐに窓に与えている値を確認してください。 – Vityata

+0

を、それがループ内で立ち往生しています。 CurRowは常にゼロより大きく、パススルーされるたびに、「For Each GetElem In Doc.getElementsByTagName( "input")」がすべて開始されます。したがって、常に最初のインスタンスに移動します。 Do While CurRow> 0ループを取り除いてください。すべての.query名を配列に入れてからi = lbound(myArray)to ubound(myArray)を使用してください。 –

+0

私は理解できません: CurRowが常に0より大きいと言われたのはなぜですか?私はCurRowがシートとして開始することを意味します1.usedrange.rows.countは78で、Do-Whileループでループするたびに1ずつ減少します – jay

答えて

0
Public Sub TEST() 
TestPage = "http://test.com/" 

Dim IE As New InternetExplorer 
Dim Doc As HTMLDocument 
Dim GetElem As Object 
Dim GetElem2 As Object 

IE.Visible = True 
IE.navigate TestPage 
Do 
    DoEvents 
Loop Until IE.readyState = READYSTATE_COMPLETE 
Application.Wait (Now + TimeValue("0:00:04")) 

Set Doc = IE.document 
CurRow = Sheet1.UsedRange.Rows.Count 

    For Each GetElem In Doc.getElementsByTagName("input") 
     If GetElem.ID = "query" Then 'I could just do getElementByID later 
      GetElem.Value = Sheet1.Range("A" & CurRow).Value 
      For Each GetElem2 In Doc.getElementsByTagName("button") 
       If GetElem2.Type = "submit" Then 
        GetElem2.Click 
        Application.Wait (Now + TimeValue("0:00:03")) 
       End If 
      Next GetElem2 
     End If 
     CurRow = CurRow + 1 
    Next GetElem 

End Sub 
関連する問題