Vba 更改IF语句以提高效率
我有很多场景会导致相同的消息框警报。 是否有比多个if语句更简单/更好的解决方案Vba 更改IF语句以提高效率,vba,excel,Vba,Excel,我有很多场景会导致相同的消息框警报。 是否有比多个if语句更简单/更好的解决方案 PRODUCTS BOX1 BOX2 BOX3 -------------------------------------------------- |Apples, Oranges, | X | x | | |Grapes, Peaches | x | x |
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
对!!您可以编写要调用的公共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
对!!您可以编写要调用的公共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
您可以将这些值保存在数组中并从中进行检查。大概是这样的:
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
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
运行
TestMe
。最初,您可以使用elseif,但它不会节省您很多时间,我认为这样更好。^^。您可以将值保存在数组中并从中进行检查。大概是这样的:
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
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
运行
TestMe
。最初,您可以使用elseif,但它不会节省您很多时间,我认为这样做更好。^^。您可以这样做:
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
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箱”、“第2箱”、“第3箱”:
如果product=“小麦”或box=“box2”或(产品“花生”和box“box3”),则
MsgBox“您的邮箱可能需要批准”
如果结束
结束选择
您可以这样做:
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
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箱”、“第2箱”、“第3箱”:
如果product=“小麦”或box=“box2”或(产品“花生”和box“box3”),则
MsgBox“您的邮箱可能需要批准”
如果结束
结束选择
<>代码>如果你的代码按预期工作,并且你正在寻找更好的方法来做同样的事情,那么考虑一下你的真实的、实际的、工作的代码。(注意:假设/伪代码不会飞过去)查看Select Case语句。组织起来可能更容易。至少,道格建议将MSGBOX()放入子程序中。如果您的代码按预期工作,并且您正在寻找更好的方法来做同样的事情,那么请考虑描述您的实际的、实际的、工作的代码。(注意:假设/伪代码不会飞过去)查看Select Case语句。组织起来可能更容易。至少,按照Doug的建议将msgbox()放入子例程中。