2017-07-25 19 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