更改IF语句更有效

时间:2016-12-05 20:25:10

标签: vba excel-vba excel

我有很多场景导致相同的消息框警报。 有没有比制作几个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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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,但它不会节省你很多时间,我认为这样做更好^^。

答案 2 :(得分:0)

是的!您可以编写一个要调用的公共子

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