gpt4 book ai didi

excel - VBA中的if-then语句

转载 作者:行者123 更新时间:2023-12-04 22:09:14 25 4
gpt4 key购买 nike

我目前正在编写两组非常相似的 if-else 语句。他们基本上比较三个下拉菜单,并确保用户没有放下两个匹配的偏好。例如:

cbo_fac1 - Kitchen    
cbo_fac2 - Kitchen
cbo_fac3 - Lounge

这将返回一条错误消息,因为 cbo_fac1 cbo_fac2 配对。但是有一个我正在努力实现的特殊情况。下拉案例之一是无偏好。
cbo_fac1 - Kitchen    
cbo_fac2 - No preference
cbo_fac3 - No preference

cbo_fac1 - No preference
cbo_fac2 - No preference
cbo_fac3 - No preference

任何情况下都不允许匹配任何偏好选择。我该如何实现呢?这是我到目前为止使用的代码:
If cbo_fac1.Value = cbo_fac2.Value Then
MsgBox ("Facilities preference 1 and facilities preference 2 cannot be the same. Please select another option for facilities preference 2, if you have none then select 'No preference'")
Exit Sub
End If

If cbo_fac1.Value = cbo_fac3.Value Then
MsgBox ("Facilities preference 1 and facilities preference 3 cannot be the same. Please select another option for facilities preference 3, if you have none then select 'No preference'")
Exit Sub
End If

If cbo_fac2.Value = cbo_fac3.Value Then
MsgBox ("Facilities preference 2 and facilities preference 3 cannot be the same. Please select another option for facilities preference 3, if you have none then select 'No preference'")
Exit Sub
End If

最佳答案

如果你想把它写成一个巨大的 if 语句,应该这样做:

If (cbo_fac1.Value <> cbo_fac2.Value Or cbo_fac1.Value = "No Preference") And _
(cbo_fac2.Value <> cbo_fac3.Value Or cbo_fac2.Value = "No Preference") And _
(cbo_fac1.Value <> cbo_fac3.Value Or cbo_fac3.Value = "No Preference") Then
'Input is fine
else
exit sub

End If

编辑:

只是因为这是相反的方式,可能带有一个 msgbox:
If (cbo_fac1.value = cbo_fac2.value AND cbo_fac1.value <> "No Preference") OR _
(cbo_fac2.value = cbo_fac3.value AND cbo_fac2.value <> "No Preference") OR _
(cbo_fac1.value = cbo_fac3.value AND cbo_fac3.value <> "No Preference") then

Msgbox "No two facilities can be the same. Please select another option " & _
"for facilities preference, if you have none then select 'No preference'"
exit sub
else
'input is fine
end if

关于excel - VBA中的if-then语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com