2017-07-25 69 views
0

我写了一个循环,从另一个工作表的特定单元格中提取条件。这个条件自身很好,但是当我为同一个数据集包含其他条件时,它会以某种方式将所有单元格标记为不正确。下面是我写的:具有多个条件的嵌套循环

rate_check = False 
    Do While sh_controls.Cells(j, 10) <> vbNullString 
    If sh_audit.Cells(i, 4) <= sh_controls.Range("c2") And sh_controls.Cells(j, 10) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Best Efforts" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) >= sh_controls.Range("c2") And sh_controls.Cells(j, 10) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Mandatory" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) <= sh_controls.Range("c3") And sh_controls.Cells(j, 10) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Mandatory" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) >= sh_controls.Range("c3") And sh_controls.Cells(j, 10) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Best Efforts" Then 
     rate_check = True 
    End If 

If sh_audit.Cells(i, 4) >= sh_controls.Range("e2") And sh_controls.Cells(j, 11) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Best Efforts" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) <= sh_controls.Range("e2") And sh_controls.Cells(j, 11) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Mandatory" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) >= sh_controls.Range("e3") And sh_controls.Cells(j, 11) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Mandatory" Then 
     rate_check = True 
    ElseIf sh_audit.Cells(i, 4) <= sh_controls.Range("e3") And sh_controls.Cells(j, 11) = sh_audit.Cells(i, 8) And sh_audit.Cells(i, 9) = "Best Efforts" Then 
     rate_check = True 
    End If 

这似乎是重叠在同一个数据集的条件...任何想法!!! ???

+0

这将帮助,如果你粘贴到你的问题,数据的一些截图,也如果你给我们多一点你的代码 - 例如如何计算'i'和'j'。 – YowE3K

回答

0

有了这个逻辑,如果第一组条件评估为True,那么第二组是毫无意义的。您可以使用两个结果,并在年底将它们结合起来:

rate_check = False 
Do While ... 
    check1 = False 
    If ... 
     check1 = True 
    End If 

    check2 = False 
    If ... 
     check2 = True 
    End If 

    rate_check = check1 And check2 
Loop