簡體   English   中英

Do Until循環以獲取大於某個數字的單元格的值

[英]Do Until loop to get value of a cell greater than a certain number

我希望代碼讀取'vResults(1,i)'的值,如果它大於21,我希望它將'vInputs(5,i)'的值增加1並繼續這樣做直到'vResults (1,i)'大於21。

不會發出任何錯誤,但Excel只會崩潰。

Sub CreateTestResultTableV2()
    Application.ScreenUpdating = False 'helps the code run faster

    Dim vInputs, vResults()
    Dim c As Integer, i As Integer

    'create INPUTS 
    c = Range("b5").End(xlToRight).Column
    vInputs = Range("b5", Cells(9, c))
    'determine last value in the column
    c = UBound(vInputs, 2)

    'create RESULTS 
    ReDim vResults(1 To 3, 1 To c)

    For i = 1 To c
        If vInputs(1, i) > 22 Then
            'set values
            Range("j16") = vInputs(1, i)
            Range("n12") = vInputs(3, i)
            Range("r14") = vInputs(5, i)

            'copy output values into RESULTS 
            vResults(1, i) = Range("h41")
            vResults(2, i) = Range("k41")
            vResults(3, i) = Range("z14")

            Do Until vResults(1, i) > 21
                vInputs(5, i).Value = vInputs(5, i).Value + 1
            Loop
        End If
    Next i

    Range("c47").Resize(3, c) = vResults
    Application.ScreenUpdating = True
End Sub

do until循環重復其指令,直到測試條件變為true 在您的情況下,您的條件永遠不會改變:

Do Until vResults(1, i) > 21
    vInputs(5, i).Value = vInputs(5, i).Value + 1
Loop

您將進入循環直到vResults(1, i)大於21,但由於該值永遠不會改變,循環永遠不會結束(AKA:無限循環)。 Windows最終會說excel沒有響應,並會詢問您是否要結束進程以退出循環,這可能看起來應用程序崩潰了。

更改vResults(1, i)或更改條件以檢查將在循環中更改的其他內容(可能是vInputs(5, i) )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM