简体   繁体   English

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

[英]Excel VBA looping block of code until cell has no value

I have a block of VBA code that does exactly what I need it to do (searches a customer account in a HTML based program and pulls data to a spreadsheet) but I'd like to have the whole block of code loop and pull the same data for multiple accounts based on a column of account numbers. 我有一段完全符合我需要的VBA代码(在基于HTML的程序中搜索客户帐户,然后将数据提取到电子表格中),但我想拥有整个代码循环,并将其提取出来基于一列帐号的多个帐户的数据。 I've tried a few different types of loops but can't seem to get the loop to work with the rowData variable. 我尝试了几种不同类型的循环,但似乎无法使循环与rowData变量一起使用。 It seems so simple (and I'm sure it is) but I just can't see it. 看起来很简单(我确定是这样),但是我看不到它。 Any help would be very much appreciated. 任何帮助将不胜感激。

Here's the part of the block (with comments) I'd like to loop... 这是我想循环播放的部分代码(带有注释)...

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

Again, this seems so simple but every loop I've tried just doesn't seem to work for me... 再说一次,这似乎很简单,但是我尝试过的每个循环似乎都不适合我...

Thanks so much! 非常感谢!

Use a for-loop. 使用for循环。 Assuming the account numbers are in column 6: 假设帐号在第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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM