当值为true时停止循环

时间:2016-08-02 19:29:13

标签: excel vba excel-vba

我想在excel中循环,直到值为true,但我怎么能完成循环?

testdb=# SELECT myngram('abcpqrs', 4);
                             myngram                             
-----------------------------------------------------------------
 {"   a","  ab"," abc",abcp,bcpq,cpqr,pqrs,"qrs ","rs  ","s   "}
(1 row)

testdb=# SELECT myngram('abcpqrs', 5);
                                       myngram                                       
-------------------------------------------------------------------------------------
 {"    a","   ab","  abc"," abcp",abcpq,bcpqr,cpqrs,"pqrs ","qrs  ","rs   ","s    "}
(1 row)

2 个答案:

答案 0 :(得分:2)

如果要迭代columnNo列中的行,则需要声明该变量并在每个循环上递增值。然后测试您的AS列是TRUE还是FALSE

Sub doUntil ()
'Do until the cells value is True.
    Dim columnNo As Integer, rowno as integer
    Calculate
    'Refreshing the database.

    'this is the column "AS" that has True/False values
    columnNo = 45

    'This is your starting row
    rowno=2

    'The True or False Cells are in the AS column
    'Starting at row number rowNo loop until we find "True" in column 45/AS
    Do Until Cells(rowno, columnNo).value = "True"

        'Paint the cell
        Cells(rowno, columnno).Interior.Color = RGB( 253, 0, 0)

        'increment your rowNo to the next row
        rowNo = rowNo + 1
    Loop 
End Sub

这里的重大变化是我们从2开始遍历所有行,直到我们遇到列AS中的值为“True”的行。如果你没有增加你的行,那么你的循环将永远不会退出,这很糟糕。

或者你可以在For循环中执行此操作:

 Sub doUntil()
       Dim columnNo as Integer, rowNo as Integer
       Calculate

       columnNo = 45

       For rowNo = 2 to 200 'start and end for the loop
            If Cells(rowNo, columnNo).value <> "True" Then
                Cells(rowno, columnNo).Interior.Color = RGB(253,0,0)
            Else
                Exit For
            End IF
       Next RowNo
 End Sub

或者在for循环中使用范围对象......

 Sub doUntil()
       Dim rngColumn as Range, rngCell
       Calculate

       set rngColumn = Columns(45)

       For each rngCell in rngColumn.Cells
            If rngCell.value <> "True" Then
                rngCell.Interior.Color = RGB(253,0,0)
            Else
                Exit For
            End IF
       Next RowNo
 End Sub

答案 1 :(得分:0)

如果你的目的是将从单元格“AS2”中包含“FALSE”的所有单元格变为红色,直到最后一个连续的“FALSE”,那么你可以避免循环并按如下方式操作:

Option Explicit

Sub doUntil()
    Dim f As range

    Calculate 'Refreshing the database.
    With Worksheets("MyLoopSheet") '<--| change "MyLoopSheet" with your actual sheet name
        If .range("AS2") Then Exit Sub '<--| if the first value is "TRUE" then you have nothing to do!
        With .range("AS2", .Cells(.Rows.Count, "AS").End(xlUp)) '<--| consider column "AS" cells from row 2 down to its last non empty one
            Set f = .Find(What:=True, LookIn:=xlValues, lookat:=xlWhole) '<--| let's look for the 1st "TRUE" in the column and consequently grab the last consecutive "False" as the preceeding cell
            If f Is Nothing Then Set f = .Rows(.Rows.Count + 1) '<--| if there's no "TRUE" value in the column then set 1st virtual "TRUE" cell one after the last
            .Resize(f.Row - 2).Interior.Color = RGB(253, 0, 0) '<--| color all "FALSE" cells in one go
        End With
    End With
End Sub

避免循环并一次性对单元格起作用可以显着减少计算时间

相关问题