2

这是我的代码。代码本身似乎可以工作,但由于某种原因,条件语句在第 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
4

1 回答 1

1

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

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

我认为afor-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
于 2013-03-06T12:02:42.150 回答