1

私は現在、非常によく似た2セットのif-elseステートメントを書いています。基本的に3つのドロップダウンメニューを比較し、ユーザーが2つの一致する設定を入力していないことを確認します。例えば:

cbo_fac1 - Kitchen    
cbo_fac2 - Kitchen   
cbo_fac3 - Lounge

cbo_fac1cbo_fac2が一致するため、これはエラーメッセージを返します。しかし、私が実装するのに苦労している特別なケースのシナリオがあります。ドロップダウンのケースの1つは、設定なしです。

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
4

2 に答える 2

3

それを 1 つの巨大な 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

編集:

可能性のあるメッセージボックスを使用して、これが逆の方法であるという理由だけで:

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
于 2012-10-24T18:44:02.657 に答える
0

のようなものを確認してください

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 つの IF 条件についても同じチェックを行います。

アップデート:

関数を書くことによって、別の方法があります

以下はサンプル関数です。

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つの組み合わせに対してこの関数を呼び出す

PreferenceCheck(cbo_fac1.Value, cbo_fac2.Value)
PreferenceCheck(cbo_fac2.Value, cbo_fac3.Value)
PreferenceCheck(cbo_fac1.Value, cbo_fac3.Value)
于 2012-10-24T18:22:32.607 に答える