Excel VBA循环代码块,直到单元格没有值

时间:2016-03-17 11:35:04

标签: excel vba excel-vba

我有一块VBA代码可以完全满足我的需求(在基于HTML的程序中搜索客户帐户并将数据提取到电子表格中)但是我希望拥有整个代码块循环并根据一列帐号为多个帐户提取相同的数据。我尝试了几种不同类型的循环,但似乎无法使循环与rowData变量一起使用。它似乎很简单(我确定它是)但我无法看到它。任何帮助将非常感谢。

这是块的一部分(带注释)我想循环...

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

同样,这看起来很简单,但我尝试过的每一个循环似乎都不适合我......

非常感谢!

1 个答案:

答案 0 :(得分: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