2016-12-05 127 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,但它不会节省你很多时间,我认为这样更好。^^。