VBA For Loop,其中每个答案必须为True

时间:2018-06-25 18:10:34

标签: excel-vba loops for-loop if-statement vba

我正在使用一个VBA宏,该宏正在运行多个循环。

但是,我要坚持的是,我希望宏遍历所有m,如果全部为True,则继续进行下一组代码。如果任何一个为假,那么我希望它进入嵌入它的下一个For循环。

         For m = 2 To 29
            If Worksheets("Current Content Analysis").Cells(contentrownum, contentcolnum) = False And _
            (Worksheets("Keyword Categorization").Cells(keywordrownum, m) = Worksheets("Product Categorization").Cells(productrownum, (m + 1)) Or _
            Worksheets("Keyword Categorization").Cells(keywordrownum, m) = "All") Then Next m

以上是我要识别的特定代码段,对于每m个,条件是否为False。如果任何一个为False,则它应继续到外循环中的下一个j。

下面是完整的循环代码(如果需要)。

完整代码:

'outer loops through each ASIN (i)
 For Each i In Worksheets("Background Search Term Analysis").Range("B5", Worksheets("Background Search Term Analysis").Cells(LastRow, 2)).Cells
    contentrownum = Application.Match(i, Worksheets("Current Content Analysis").Range("B1", Worksheets("Current Content Analysis").Cells(LastRow, 2)).Cells, 0)
    productrownum = Application.Match(i, Worksheets("Product Categorization").Range("A1", Worksheets("Product Categorization").Cells(LastRow, 1)).Cells, 0)

    'inner loops through each keyword (j)
    For Each j In Worksheets("Current Content Analysis").Range("M2", Worksheets("Current Content Analysis").Cells(2, LastColumn)).Cells
        contentcolnum = Application.Match(j, Worksheets("Current Content Analysis").Range("A2", Worksheets("Current Content Analysis").Cells(2, LastColumn)).Cells, 0)
        keywordrownum = Application.Match(j, Worksheets("Keyword Categorization").Range("A1", Worksheets("Keyword Categorization").Cells(LastKeywordRow, 1)).Cells, 0)

        'prints the current values for each variable as the loop progresses
        'end values should match printed last row and column; helps identify where breaks occur
        Worksheets("Current Content Analysis").Cells(1, 4).Value = contentrownum
        Worksheets("Current Content Analysis").Cells(1, 5).Value = productrownum
        Worksheets("Current Content Analysis").Cells(1, 6).Value = contentcolnum
        Worksheets("Current Content Analysis").Cells(1, 7).Value = keywordrownum

       'if this product doesn't currently have the keyword (j) in it and the keyword tags match the product tags then
       For m = 2 To 29
        If Worksheets("Current Content Analysis").Cells(contentrownum, contentcolnum) = False And _
        (Worksheets("Keyword Categorization").Cells(keywordrownum, m) = Worksheets("Product Categorization").Cells(productrownum, (m + 1)) Or _
        Worksheets("Keyword Categorization").Cells(keywordrownum, m) = "All") Then Next m 

        'if all of m loop are True, then move on to this step; if any m is False, then move on to next j
        WrdArray() = Split(j)
            For k = LBound(WrdArray) To UBound(WrdArray)
                If InStr(LCase(result_no_dup), LCase(WrdArray(k))) = 0 _
                And InStr(LCase(Worksheets("Current Content Analysis").Cells(contentrownum, 12).Value), LCase(WrdArray(k))) = 0 Then
                    result_no_dup_compact = result_no_dup_compact & WrdArray(k)
                    If Len(result_no_dup_compact) > Worksheets("Instructions").Range("B4").Value Then Exit For
                    result_no_dup = result_no_dup & " " & WrdArray(k)
                End If
            Next k
            result_no_spaces = result & j
            If Len(result_no_spaces) > Worksheets("Instructions").Range("B4").Value Then Exit For
            'if true and character limit not exceeded, add the keyword (j) to our result concatentation
            result = result & " " & j
            'if true and character limit not exceeded, add the keyword (j) to our results concatenation with commas
            result_commas = result_commas & ", " & j
        End If
    Next j

   'once i go through all of my keywords, set ASIN background search term cell value equal to result
   Worksheets("Background Search Term Analysis").Cells(contentrownum, 4).Value = Right(result, Len(result) - 1)
   Worksheets("Background Search Term Analysis").Cells(contentrownum, 5).Value = Right(result_commas, Len(result_commas) - 2)
   Worksheets("Background Search Term Analysis").Cells(contentrownum, 6).Value = Right(result_no_dup, Len(result_no_dup) - 1)
   'reset results to empty for next ASIN (i)
   result = ""
   result_no_spaces = ""
   result_commas = ""
   result_no_dup = ""
   result_no_dup_compact = ""
Next i

谢谢您的帮助!

2 个答案:

答案 0 :(得分:3)

If you find an m condition that doesn't match, Exit For. The value of m will be <=29. It will only be >29 if m has looped successfully through 2 to 29 because the last Next m increments m to 30 and that's when it exits the For m = 2 To 29 loop.

' ...
'inner loops through each keyword (j)
For Each j In Worksheets("Current Content Analysis").Range("M2", Worksheets("Current Content Analysis").Cells(2, LastColumn)).Cells
    ' ...
   'if this product doesn't currently have the keyword (j) in it and the keyword tags match the product tags then
   For m = 2 To 29
       If cbool(Worksheets("Current Content Analysis").Cells(contentrownum, contentcolnum)) then
           exit for
       elseif Worksheets("Keyword Categorization").Cells(keywordrownum, m) <> Worksheets("Product Categorization").Cells(productrownum, (m + 1)) and _
              Worksheets("Keyword Categorization").Cells(keywordrownum, m) <> "All") then
           exit for
       end if
   Next m 

    'if all of m loop are True, then move on to this step; if any m is False, then move on to next j
    if m > 29 then
        ' ...
    End If
Next j

答案 1 :(得分:2)

进行循环,如果发现False,则将其记录为布尔值。

测试布尔值,如果找不到则执行代码:

Dim fndflse As Boolean
fndflse = False
For m = 2 To 29
    If Worksheets("Current Content Analysis").Cells(contentrownum, contentcolnum) = False And _
    (Worksheets("Keyword Categorization").Cells(keywordrownum, m) = Worksheets("Product Categorization").Cells(productrownum, (m + 1)) Or _
    Worksheets("Keyword Categorization").Cells(keywordrownum, m) = "All") Then
        fndflse = True
        Exit For
    End If
Next m
If Not fndflse Then
    'your code it all true
End If