2016-12-05 125 views
1

我有很多導致相同消息框警報的場景。 是否有比製作幾個if語句更容易/更好的解決方案?更改IF語句以提高效率

 PRODUCTS   BOX1  BOX2  BOX3 
    -------------------------------------------------- 
    |Apples, Oranges, | X | x |   | 
    |Grapes, Peaches | x | x |   | 
    |------------------------------------------------| 
    |Wheat   | x | x |  x | 
    |------------------------------------------------- 
    |Peanuts   |   | x |   | 
    -------------------------------------------------- 

If product = "Apples" or product = Oranges or product = Grapes or products = Peaches then 
    If box = "box1" or box = "box2" then 
     msgbox "Your box may require approval" 
    End If 
End If 

If product = "Wheat" then 
    If box = "box1" or box = "box2" or box = "box3" then 
     msgbox "Your box may require approval" 
    End If 
End If 

If product = "Peanuts" then 
    If box = "box2" then 
     msgbox "Your box may require approval" 
    End If 
End If 
+2

如果你的代碼按預期工作,而你正在尋找更好的方法做同樣的事情,然後再考慮你的描述真實*,實際的工作*代碼在[codereview.se]上。 (注意:假設/僞代碼不會飛過) –

+1

查看Select Case語句。組織起來可能更容易。至少,按照Doug的建議,將msgbox()放入子程序中。 – B540Glenn

回答

3

你可以做這樣的:

Select Case product 
Case "Apples", "Oranges", "Grapes", "Peaches", "Wheat", "Peanuts" 
    Select Case box 
    Case "box1", "box2", "box3": 
     If product = "Wheat" Or box = "box2" Or (product <> "Peanuts" And box <> "box3") Then 
      MsgBox "Your box may require approval" 
     End If 
End Select 
0

是的!你可以寫一個公共的Sub來調用

If product = "Apples" or product = Oranges or product = Grapes or products = Peaches then 
    If box = "box1" or box = "box2" then 
     Call MySub 
    End If 
End If 

If product = "Wheat" then 
    If box = "box1" or box = "box2" or box = "box3" then 
     Call MySub 
    End If 
End If 

If product = "Peanuts" then 
    If box = "box2" then 
     Call MySub 
    End If 
End If 

Public Sub MySub 
    msgbox "Your box may require approval" 
End Sub 
1

你可以保留數組中的值並從那裏檢查。類似這樣的:

Option Explicit 

Public Function b_value_in_array(my_value As Variant, my_array As Variant) As Boolean 

    Dim l_counter as long 

    For l_counter = LBound(my_array) To UBound(my_array) 
     my_array(l_counter) = CStr(my_array(l_counter)) 
    Next l_counter 

    b_value_in_array = Not IsError(Application.Match(CStr(my_value), my_array, 0)) 

End Function 

Public Sub TestMe() 

    Dim product   As String: product = "Oranges" 
    Dim box    As String: box = "box2" 

    Dim arr_products1 As Variant 
    Dim arr_products2 As Variant 
    Dim arr_products3 As Variant 
    Dim arr_boxes_1  As Variant 
    Dim arr_boxes_2  As Variant 
    Dim arr_boxes_3  As Variant 

    arr_products1 = Array("Apples", "Oranges", "Grapes", "Peaches") 
    arr_products2 = Array("Wheat") 
    arr_products3 = Array("Peanuts") 

    arr_boxes_1 = Array("box1", "box2") 
    arr_boxes_2 = Array("box1", "box2", "box3") 
    arr_boxes_3 = Array("box2") 

    If b_value_in_array(product, arr_products1) And b_value_in_array(box, arr_boxes_1) Then 
     Call ApprovalMsgBox 
    End If 

    If b_value_in_array(product, arr_products2) And b_value_in_array(box, arr_boxes_2) Then 
     Call ApprovalMsgBox 
    End If 

    If b_value_in_array(product, arr_products3) And b_value_in_array(box, arr_boxes_3) Then 
     Call ApprovalMsgBox 
    End If 

End Sub 

Public Sub ApprovalMsgBox() 
    MsgBox "Your box may require approval" 
End Sub 

運行TestMe。最初,你可以使用elseif,但它不會節省你很多時間,我認爲這樣更好。^^。