2016-08-02 11 views
-1

ループが実行されるのは、値がtrueになるまで有効ですが、どのようにループを終了できますか?値がtrueのときにループを停止

Sub doUntil() 
'Do until the cells value is True. 
    Dim columnNo As Integer 
    Calculate 
    'Refreshing the database. 
    columnNo = 45 
    'The True or False Cells are in the AS column 
    Do 
    Cells(columnNo, 45).Interior.Color = RGB(253, 0, 0) 
    Loop Until True 
End Sub 
+0

あなたは 'columnNo'変数をどんな形でも増やしていません... – Chrismas007

+0

これはプログラミングではない方法では条件付き書式を使うべきです質問。 – Chrismas007

+0

それは1つの列にあります。 –

答えて

2

columnNo列の行を反復処理する場合は、その変数を宣言して各ループの値を増やす必要があります。あなたのAS列がTRUEFALSEされている場合は、テスト:

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 

ここに大きな変化は、我々はそれに「真」の欄ASが値を持つ行を打つまで、2から始まる、あなたのすべての行を反復処理しているということです。行をインクリメントしないと、ループは決して終了せず、それは悪いことです。

別の方法としては、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 

またはループのためにある範囲のオブジェクトを使用して...

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 
+0

悪い質問への良い答え。 – Chrismas007

+0

実際には最高の応答!ありがとうございましたJNevillと@ Chrismas007あなたの恩人のための私のばかげた質問のために私を許してください... –

+0

それについて悪い唯一の事は、ループがインターネットの周りの多くの例で大きく文書化されて以来、しかし...初めてプログラミングのコンセプトに取り組んでいるような気がします。それは確かに圧倒的です。 – JNevill

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 

ループを回避して一気にセルに作用すると、計算時間が大幅に短縮されます

+0

@DominikMajsai:あなたはそれを通過しましたか? – user3598756

+0

@DominikMajsai:あなたが試して助けてくれる人にフィードバックを与えるのはいいことです – user3598756

関連する問題