VBA 在 700 次迭代左右崩溃 - 当我尝试将此 VBA 循环运行到 1000 次(650 次是安全的)迭代时,Excel 不断崩溃。有什么想法吗?

问题描述 投票:0回答:1
  Sub LottoSearch()
   Dim stopRunUp As Integer
   stopRunUp = 0
    For i = 2 To 1000

     lottoNum = Cells(i, 3).Value

      If lottoNum = 3957481 Then
        Cells(2, 6).Value = Cells(i, 1).Value
        Cells(2, 7).Value = Cells(i, 2).Value
        Cells(2, 8).Value = lottoNum
        MsgBox ("Congratulations " + Str(Cells(2, 6).Value) + " " + Str(Cells(2, 7).Value))
      End If

      If lottoNum = 5865187 Then
       Cells(3, 6).Value = Cells(i, 1).Value
       Cells(3, 7).Value = Cells(i, 2).Value
       Cells(3, 8).Value = lottoNum
      End If

     If lottoNum = 2817729 Then
       Cells(4, 6).Value = Cells(i, 1).Value
       Cells(4, 7).Value = Cells(i, 2).Value
       Cells(4, 8).Value = lottoNum
     End If

     If lottoNum = 2275339 And stopRunUp = 0 Then
       Cells(5, 6).Value = Cells(i, 1).Value
       Cells(5, 7).Value = Cells(i, 2).Value
       Cells(5, 8).Value = lottoNum
       stopRunUp = 1
     End If

     If lottoNum = 5868182 And stopRunUp = 0 Then
       Cells(5, 6).Value = Cells(i, 1).Value
       Cells(5, 7).Value = Cells(i, 2).Value
       Cells(5, 8).Value = lottoNum
       stopRunUp = 1
     End If

     If lottoNum = 1841402 And stopRunUp = 0 Then
       Cells(5, 6).Value = Cells(i, 1).Value
       Cells(5, 7).Value = Cells(i, 2).Value
       Cells(5, 8).Value = lottoNum
       stopRunUp = 1
     End If

   Next i

 End Sub

我已仔细检查我是否拥有 Excel (Microsoft 365) 的更新版本。

确保有足够的内存空间和CPU。

在 Excel 业务和家庭中尝试过,看看是否有什么不同,没有运气。

只要我的 for 循环限制为 650 或更低,代码就可以工作。

还有其他人遇到过这个问题吗?

excel vba for-loop crash
1个回答
0
投票
Option Explicit
Sub LottoSearch()
    Dim stopRunUp As Integer
    Dim lastRow As Long, lottoNum
    Dim i As Long, j As Long
    Dim arrData, rngData As Range
    lastRow = 1000
    stopRunUp = 0
    Set rngData = Range("A1:H" & lastRow)
    arrData = rngData.Value
    For i = LBound(arrData) + 1 To UBound(arrData)
        lottoNum = arrData(i, 3)
        If lottoNum = 3957481 Then
            arrData(2, 6) = arrData(i, 1)
            arrData(2, 7) = arrData(i, 2)
            arrData(2, 8) = lottoNum
            MsgBox ("Congratulations " + Str(arrData(2, 6)) + " " + Str(arrData(2, 7)))
        ElseIf lottoNum = 5865187 Then
            arrData(3, 6) = arrData(i, 1)
            arrData(3, 7) = arrData(i, 2)
            arrData(3, 8) = lottoNum
        ElseIf lottoNum = 2817729 Then
            arrData(4, 6) = arrData(i, 1)
            arrData(4, 7) = arrData(i, 2)
            arrData(4, 8) = lottoNum
        ElseIf lottoNum = 2275339 And stopRunUp = 0 Then
            arrData(5, 6) = arrData(i, 1)
            arrData(5, 7) = arrData(i, 2)
            arrData(5, 8) = lottoNum
            stopRunUp = 1
        ElseIf lottoNum = 5868182 And stopRunUp = 0 Then
            arrData(5, 6) = arrData(i, 1)
            arrData(5, 7) = arrData(i, 2)
            arrData(5, 8) = lottoNum
            stopRunUp = 1
        ElseIf lottoNum = 1841402 And stopRunUp = 0 Then
            arrData(5, 6) = arrData(i, 1)
            arrData(5, 7) = arrData(i, 2)
            arrData(5, 8) = lottoNum
            stopRunUp = 1
        End If
    Next i
    rngData.Value = arrData
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.