2016-03-17 91 views
0

我有一塊VBA代碼,它完全符合我需要的操作(在基於HTML的程序中搜索客戶帳戶並將數據提取到電子表格) d喜歡讓整個代碼塊循環並根據一列帳號爲多個帳戶提取相同的數據。我嘗試了幾種不同類型的循環,但似乎無法使循環與rowData變量一起工作。看起來很簡單(我確信它是這樣),但我看不到它。任何幫助將非常感激。Excel VBA循環代碼塊直到單元格沒有值

這裏是塊(含評論)我想循環的一部分......

rowData = 6 'set it to the first row of player account data 
dblTotalS = 0 
dblTotalT = 0 

'START LOOP HERE using rowData variable to check if column is empty 

' Input the account number & click search 
IE.document.getElementById("accountNumber").Value = Me.Cells(rowData, 6).Value 'use the rowdata variable to get which row we are at 
IE.document.getElementById("action").Click 
If Not IEWait(IE) Then 
    GoTo EndMe 
End If 

' navigate to the activity page 
IE.navigate my_url3 
If Not IEWait(IE) Then 
    GoTo EndMe 
End If 

' input search criteria 
IE.document.getElementById("site").Value = Me.Cells(7, 4).Value 
IE.document.getElementById("action").Click 
If Not IEWait(IE) Then 
    GoTo EndMe 
End If 

dblCustomerTTotal = 0 
dblCustomerSTotal = 0 
For i = 1 To 1 
    Set TDelements = IE.document.getElementsByTagName("tr") 
    For Each TDelement In TDelements 
     If TDelement.className = "searchActivityResultsCustomerTContent" Then 
      dblCustomerTTotal = dblCustomerTTotal + VBA.CDbl(TDelement.ChildNodes(8).innerText) 
     ElseIf TDelement.className = "searchActivityResultsCustomerSContent" Then 
      dblCustomerSTotal = dblCustomerSTotal + VBA.CDbl(TDelement.ChildNodes(8).innerText) 
     End If 
    Next 
    Set elems = IE.document.getElementsByTagName("input") 
    For Each e In elems 
     If e.Value = "Next Results" Then 
      e.Click 
      If Not IEWait(IE) Then 
       GoTo EndMe 
      End If 
      i = 0 
      Exit For 
     End If 
    Next e 
Next i 
Me.Cells(rowData, 7).Value = dblCustomerTTotal 
Me.Cells(rowData, 8).Value = dblCustomerSTotal 
Me.Cells(rowData, 9).Value = dblCustomerTTotal + dblCustomerSTotal 
dblTotalT = dblTotalT + dblCustomerTTotal 
dblTotalS = dblTotalS + dblCustomerSTotal 
' 
' END LOOP HERE 

EndMe: 
IE.Quit 
On Error GoTo 0 'reset the error handler 
End Sub 

再次,這看起來很簡單,但每次我只是試着循環似乎不工作對我來說......

非常感謝!

回答

0

使用for循環。假設帳號在第6列:

Dim lastRow As Integer 

lastRow = Cells(10000, 6).End(xlUp).Row 

rowData = 6 'set it to the first row of player account data 
dblTotalS = 0 
dblTotalT = 0 

'START LOOP HERE using rowData variable to check if column is empty 
For x = rowData To lastRow 

    ' Input the account number & click search 
    IE.document.getElementById("accountNumber").Value = Me.Cells(x, 6).Value 'use the rowdata variable to get which row we are at 
    IE.document.getElementById("action").Click 
    If Not IEWait(IE) Then 
     GoTo EndMe 
    End If 

    ' navigate to the activity page 
    IE.navigate my_url3 
    If Not IEWait(IE) Then 
     GoTo EndMe 
    End If 

    ' input search criteria 
    IE.document.getElementById("site").Value = Me.Cells(7, 4).Value 
    IE.document.getElementById("action").Click 
    If Not IEWait(IE) Then 
     GoTo EndMe 
    End If 

    dblCustomerTTotal = 0 
    dblCustomerSTotal = 0 
    For i = 1 To 1 
     Set TDelements = IE.document.getElementsByTagName("tr") 
     For Each TDelement In TDelements 
      If TDelement.className = "searchActivityResultsCustomerTContent" Then 
       dblCustomerTTotal = dblCustomerTTotal + VBA.CDbl(TDelement.ChildNodes(8).innerText) 
      ElseIf TDelement.className = "searchActivityResultsCustomerSContent" Then 
       dblCustomerSTotal = dblCustomerSTotal + VBA.CDbl(TDelement.ChildNodes(8).innerText) 
      End If 
     Next 
     Set elems = IE.document.getElementsByTagName("input") 
     For Each e In elems 
      If e.Value = "Next Results" Then 
       e.Click 
       If Not IEWait(IE) Then 
        GoTo EndMe 
       End If 
       i = 0 
       Exit For 
      End If 
     Next e 
    Next i 
    Me.Cells(rowData, 7).Value = dblCustomerTTotal 
    Me.Cells(rowData, 8).Value = dblCustomerSTotal 
    Me.Cells(rowData, 9).Value = dblCustomerTTotal + dblCustomerSTotal 
    dblTotalT = dblTotalT + dblCustomerTTotal 
    dblTotalS = dblTotalS + dblCustomerSTotal 
    ' 
    ' END LOOP HERE 
Next x 

EndMe: 
IE.Quit 
On Error GoTo 0 'reset the error handler 

End Sub 
+0

非常感謝!這很好,不得不改變代碼的位置,但像魅力一樣工作。我現在唯一的問題是,當它導入數據(「Me.Cells(rowdata ...」)行時,它每次都會覆蓋數據。是否有一種簡短/快速的方法使它向下移動每一組數據或甚至將數據與帳戶號碼對齊?對不起,這又似乎很簡單,但我不能看到一種方法來做到這一點,而不必設置更多變量來說明每次循環移動行時。再次感謝! –

+0

對不起,沒有看到最後一點,你現在使用變量x來代替rowData - 每次循環迭代x增加1,所以用x替換rowData例如Me.Cells(rowData,7) .Value = dblCustomerTTotal變爲Me.Cells(x,7).Value = dblCustomerTTotal – Absinthe

+0

太棒了!非常感謝幫助@Absinthe –