首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果-然后语句在VBA中

如果-然后语句在VBA中
EN

Stack Overflow用户
提问于 2012-10-24 18:17:50
回答 2查看 10.7K关注 0票数 1

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

代码语言:javascript
运行
复制
cbo_fac1 - Kitchen    
cbo_fac2 - Kitchen   
cbo_fac3 - Lounge

这将返回一条错误消息,因为cbo_fac1cbo_fac2匹配。但有一个特殊的情况,我正在努力实现。其中一个下降的情况是没有偏好。

代码语言:javascript
运行
复制
cbo_fac1 - Kitchen    
cbo_fac2 - No preference
cbo_fac3 - No preference

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

在任何情况下,都不允许首选项选择匹配。我该如何实现这一点呢?下面是我到目前为止使用的代码:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-24 18:44:02

如果您想把它写成一个巨大的If语句,那么应该这样做:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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
票数 3
EN

Stack Overflow用户

发布于 2012-10-24 18:22:32

检查一下类似的

代码语言:javascript
运行
复制
If cbo_fac1.Value = cbo_fac2.Value and cbo_fac1.Value <> "No Preference" and cbo_fac2.Value <> "No Preference" 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

对其他2种情况执行相同的检查。

更新:

还有另一种方法,通过编写函数

下面是示例函数。

代码语言:javascript
运行
复制
Sub PreferenceCheck(var1 As String, var2 As String)
    If var1 = "No Preference" Or var2 = "No Preference" Then
        tempVar = true
    End If
    If Not tempVar Then
        If var1=var2 Then
            MsgBox ("Facilities "&var1&" and facilities "&var2&" cannot be the same. Please select another option for facilities preference, if you have none then select 'No preference'")
        End If
    End If
End Sub

为3种组合调用此函数

代码语言:javascript
运行
复制
PreferenceCheck(cbo_fac1.Value, cbo_fac2.Value)
PreferenceCheck(cbo_fac2.Value, cbo_fac3.Value)
PreferenceCheck(cbo_fac1.Value, cbo_fac3.Value)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13055281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档