【问题标题】:in VBA For loop stops executing after around 530 iterations在 VBA 中 For 循环在大约 530 次迭代后停止执行
【发布时间】:2019-06-22 06:17:59
【问题描述】:

我已经为我的工作相关任务编写了简单的代码,但它在 530 迭代时停止执行而没有任何错误消息,而我仍有一些数据需要处理。

试图删除 VBA 中的所有代码并从记事本粘贴。试过调试器。尝试重启excel和pc。

Function CoRow() As Long
    CoRow = Cells(Rows.Count, 1).End(xlUp).Row
End Function

Sub Sort()
    Dim LastNace As Integer
    Dim NextNace As Integer
    Dim i As Long
    LastNace = Cells(2, "C").Value
    NextNace = Cells(3, "C").Value
    Columns("A:E").Select
    Selection.Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("E2"), Order2:=xlDescending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
    For i = 1 To CoRow
        If LastNace <> NextNace And LastNace <> 0 And NextNace <> 0 And i <> 1 Then
            Rows(i + 1).EntireRow.Insert
            Range(Cells(i + 1, 1), Cells(i + 1, 5)).Interior.Color = RGB(255, 255, 0)
            i = i + 1
        ElseIf LastNace <> NextNace And LastNace <> 0 And NextNace = 0 And i <> 1 Then
            Rows(i + 1).EntireRow.Insert
            Range(Cells(i + 1, 1), Cells(i + 1, 5)).Interior.Color = RGB(255, 255, 0)
            i = i + 1
        End If
        LastNace = Cells(i + 1, "C").Value
        NextNace = Cells(i + 2, "C").Value
        'Range(Cells(i + 1, 3).Address(), Cells(i + 1, 3).Address()).Interior.Color = RGB(255, 0, 0)
    Next i
End Sub

预期结果是超过 530 次迭代。我怀疑排序有问题,因为在执行此代码之前它也对相同数量的行进行排序。

【问题讨论】:

  • 您正在插入行并反复调用 coRow - 这是您的意图吗?您在所有容易出错的地方都使用隐式活动表引用 - 您应该在单元格/范围前面添加工作表名称(或将整个内容包装在 With 语句中并使用点运算符,例如 .Cells)。避免 .Select 并使用 Long 而不是 Integer。
  • 是的。在我插入新行后,CoRow 会重新计算我的表中有多少行。此代码由工作表数据上的按钮激活,但你是对的,我应该添加工作表名称。我需要 Selection.Sort 来对我的数据进行排序。
  • 您可以确定范围并将其用于您的排序,而不是使用 .Select
  • 我已将主程序更改如下: ... Sheets("Imp").Range("A:E").Sort Key1:=Range("C2"), Order1:=xlAscending , Key2:=Range("E2"), Order2:=xlDescending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal 但是它仍然不超过 530 次迭代。

标签: excel vba


【解决方案1】:

您对CoRow 的重新计算不会影响循环结束!

注意在For循环中,一旦循环开始

For i = 1 To CoRow

CoRow 的任何值更改不会影响循环结束For 循环始终使用循环开始时设置的CoRow 的值。

以下示例:

Dim i As Long
Dim iEnd As Long
iEnd = 10

For i = 1 To iEnd
    iEnd = 20 'this has NO EFFECT on the end of the For loop
    Debug.Print i, iEnd
Next i

此循环将仅从 1 … 10 运行,因为一旦循环以 For i = 1 To iEnd 开始,iEnd = 20 的任何更改都不会影响循环的结束。


解决方案

将其替换为 Do 循环。

Dim i As Long
Dim iEnd As Long
iEnd = 10

i = 1 'initialization needed before Do loops

Do While i <= iEnd
    iEnd = 20
    Debug.Print i, iEnd

    i = i + 1 'manual increase of counter needed in the end of Do loops
Loop

请注意,对于Do 循环,您需要初始化计数器i = 1 并手动增加它@9​​87654335@。这次iEnd = 20 的更改生效并且循环从1 … 20 运行,因为Do 循环在每次 迭代中评估条件i &lt;= iEnd(不仅在开始时作为@987654340 @loop 确实如此)。

另类

另一种解决方案(如果您插入或删除行)是向后运行循环:

Dim CoRow As Long 'make it a variable not a function then
CoRow = Cells(Row.Count, 1).End(xlUp).Row

Dim i As Long
For i = CoRow To 1 Step -1
    'runs backwards starting at the last row ending at the first
Next i

但这是否可能取决于您的数据以及您在循环中执行的操作。


改进

请注意,CoRow = Cells(Rows.Count, 1).End(xlUp).Row 会占用一些时间。与其将CoRow 设为函数,不如将其设为变量,每次插入一行时将其增加1 CoRow = CoRow + 1,这将比一遍又一遍地确定最后一行要快得多。

【讨论】:

    【解决方案2】:

    谢谢各位。我从你的建议中实现了很多,现在这段代码做了我想做的事情。 :)

    Function CoRow() As Long
    CoRow = Cells(Rows.count, 1).End(xlUp).Row
    End Function
    
    Sub Sort()
    Dim LastNace As Integer
    Dim NextNace As Integer
    Dim CountNace As Integer
    Dim r As Long
    Dim i As Long
    Sheets("Imp").Range("A:E").Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("E2"), Order2:=xlDescending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
    LastNace = Sheets("Imp").Cells(2, "C").Value
    NextNace = Sheets("Imp").Cells(3, "C").Value
    r = CoRow
    CountNace = 0
    For i = 1 To r
        If LastNace <> NextNace And LastNace <> 0 And NextNace <> 0 Then
            CountNace = CountNace + 1
        End If
        LastNace = Sheets("Imp").Cells(i + 1, "C").Value
        NextNace = Sheets("Imp").Cells(i + 2, "C").Value
    Next
    r = r + CountNace
    LastNace = Sheets("Imp").Cells(2, "C").Value
    NextNace = Sheets("Imp").Cells(3, "C").Value
    For i = 1 To r
        If LastNace <> NextNace And LastNace <> 0 And NextNace <> 0 And i <> 1 Then
            Sheets("Imp").Rows(i + 1).EntireRow.Insert
            Sheets("Imp").Range(Cells(i + 1, 1), Cells(i + 1, 5)).Interior.Color = RGB(255, 255, 0)
            i = i + 1
        ElseIf LastNace <> NextNace And LastNace <> 0 And NextNace = 0 And i <> 1 Then
            Sheets("Imp").Rows(i + 1).EntireRow.Insert
            Sheets("Imp").Range(Cells(i + 1, 1), Cells(i + 1, 5)).Interior.Color = RGB(255, 255, 0)
            i = i + 1
        End If
        LastNace = Sheets("Imp").Cells(i + 1, "C").Value
        NextNace = Sheets("Imp").Cells(i + 2, "C").Value
        'Sheets("Imp").Range(Cells(i + 1, 3), Cells(i + 1, 3)).Interior.Color = RGB(255, 0, 0)
    Next
    End Sub
    

    【讨论】:

    • 请注意,我建议再次阅读我的答案。你所做的(循环两次)需要很多时间,而且效率很低。我建议保留一个循环并将For 循环替换为Do 循环,您的代码应该会变得更快。
    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多