VBA循环条件在任意行数后停止评估

标签 vba loops excel for-loop while-loop

这是我的代码。代码本身似乎可以工作,但由于某种原因,条件语句在第 32645 行之后停止。我尝试将所有变量切换为 Long,但没有帮助。

此外,如果我让它从第 32646 行开始,然后在后面的随机行 (~18000) 停止,则代码可以工作。它停止的数据似乎没有相似之处。最初我尝试了一个指定开始行和结束行的 for 循环,但这也不起作用,所以我尝试了 while 循环(理论上两者都应该起作用,但似乎都不起作用)。

它需要能够处理 130,000 + 行,知道为什么会发生这种情况吗?

问题

循环停止运行而没有错误,通过添加一个消息框,我知道我的行递增变量在最后一行结束,但根据工作簿,条件语句在任意行数后停止评估。

如果我从第一次运行停止的下一行开始运行脚本,它可以工作,但同样适用于(可能不同的)任意数量的步骤。

笔记

我已将所有变量设为“长”类型。我使用了选项显式。

data_row = CLng(InputBox("Please enter the row number of the first data entry"))
Worksheets.Add(After:=Worksheets(1)).name = "Formatted_Output"

Set ws = Sheets("Formatted_Output")
Worksheets(1).Activate
LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).row
data_col = LastCol

 'loop through entries to get account and permissions
 increment = data_row
 Do Until increment = LastRow
    If Cells(data_row, data_col) = "" Then
        data_col = data_col - 1
    Else
   ' ~> For illegal ==> access denied permission that throws errors
        If Cells(data_row, data_col).Value = "==>access denied" Then
            permissions = "access denied" 'remove illegal =
            ws.Cells(output_row, 3).Value = permissions 'print permissions to output file
        Else
            permissions = Cells(data_row, data_col).Value 'cell should be permission cell of each row
            ws.Cells(output_row, 3).Value = permissions 'print permissions to output file
        End If

        data_col = data_col - 1 'domain / account cell is now highlighted

        If InStrRev(Cells(data_row, data_col).Value, "?") > 0 Then

            account = Split(Cells(data_row, data_col).Value, "?")(1) & Str(unknown_count) ' separate domain name and unknown id
            unknown_count = unknown_count + 1 ' counting the number of unkown accounts found
            ws.Cells(output_row, 2) = account 'print unknown account Id to output

            domain_bit = Split(Cells(data_row, data_col).Value, "?")(0) '' get separate domain name from id cell
            data_col = data_col - 1 'domain second from end cell is now highlighted

            Do While data_col > 0 'generate domain from rest of row
                domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards
                data_col = data_col - 1 'check next column for more of location name
            Loop

            ws.Cells(output_row, 1) = domain_bit
                'data_col = LastCol
                'data_row = data_row + 1
                'output_row = output_row + 1

        ElseIf InStrRev(Cells(data_row, data_col).Value, "\") > 0 Then

            account = Split(Cells(data_row, data_col).Value, "\")(1) 'separate account ID
            ws.Cells(output_row, 2) = account 'print account ID to oputput

            domain_bit = Split(Cells(data_row, data_col).Value, "\")(0)
            data_col = data_col - 1 'domain second from end cell is now highlighted

            Do While data_col > 0 'generate domain from rest of row
                domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards
                data_col = data_col - 1 'check next column for more of location name
            Loop

            ws.Cells(output_row, 1) = domain_bit 'output to file

        Else
                         account = Cells(data_row, data_col).Value 'account is just whole cell whether empty or one word no path
            ws.Cells(output_row, 2) = account 'print account ID to oputput

            data_col = data_col - 1 'domain second from end cell is now highlighted (since no domain in account cell)
            Do While data_col > 0 'generate domain from rest of row
                domain_bit = Cells(data_row, data_col).Value & domain_bit 'domain built backwards
                data_col = data_col - 1 'check next column for more of location name
            Loop

            ws.Cells(output_row, 1) = domain_bit 'output to file
        End If

    data_col = LastCol
    data_row = data_row + 1
    output_row = output_row + 1
    End If

'Next increment
ws.Range("E" & 1) = unknown_count
increment = increment + 1

If increment = LastRow Then
    MsgBox (Str(increment) & "=" & Str(LastRow))
End If

Loop

最佳答案

我相信您的第一个总体 if 语句没有按照应有的方式执行,如果发现它是真的,data_row变量不增加禁用自动行增加。

如果这是您想要的,那么 increment变量需要移动到 if 语句中,因为它会增加而与 data_row 无关多变的。

我认为 for-next会更好:

for data_row = 1 to LastRow
    'do some stuff
next

这将遍历所有行 1 到 LastRow每次迭代。

添加了更好的答案:
Do Until increment > LastRow
    ...
    data_col = LastCol
    data_row = data_row + 1
    output_row = output_row + 1
    increment = increment + 1
    End If

    If increment > LastRow Then
        MsgBox (Str(increment) & ">" & Str(LastRow))
    End If
Loop

关于VBA循环条件在任意行数后停止评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228717/

相关文章:

vba - 将excel中的工作表保存到另一个excel

java - 在循环内声明新变量如何不会出错?

Java内存不足错误: Strange behavior

vb.net - 如何使用 vb.net 以编程方式对 Excel 中的列进行排序?

excel - 有没有办法取消选中数据透视表中除特定项目之外的其他项目?

excel - 使用 Raiserror : Is this possible? 将多步 ADO 查询的进度传递到 VBA

javascript - 多次发出ajax请求

javascript - 将数据导出到 excel/csv 时设置列宽和自动换行属性

javascript - 如何使用nodejs将excel文件(XLSX)导入mysql

vba - 对象模块中已存在 Visual Basic 成员