VBA/Excel/Access/Word/Language Basics/Comparison Operators

Материал из VB Эксперт
Версия от 12:46, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Comparison operators in VBA

 
Operator     Function
=            Tests for equality
<>           Tests for inequality
<            Less than
>            Greater than
<=           Less than or equal to
>=           Greater than or equal to



If/Then/Else with Comparison operators

 
Sub NumberGuess()
    Dim userGuess As Integer
    Dim answer As Integer
    answer = Rnd * 10
    userGuess = 12
    If (userGuess > answer) Then
        MsgBox ("Too high!")
        MsgBox ("The answer is " & answer)
    End If
    If (userGuess < answer) Then
        MsgBox ("Too low!")
        MsgBox ("The answer is " & answer)
    End If
    If (userGuess = answer) Then MsgBox ("You got it!")
End Sub



<> operator

 
Sub AskForPassword2()
       Dim pWord As String
       pWord = ""
       Do While pWord <> "PASS"
           pWord = InputBox("What is the Report password?")
           If pWord = "" Then Exit Do
       Loop
       If pWord <> "" Then
           MsgBox "You entered correct password."
   End If
End Sub



Select Case with Comparison operators

 
Public Function AssignGrade(studentScore As Single) As String
    Select Case studentScore
        Case 90 To 100
            AssignGrade = "A"
        Case Is >= 80
            AssignGrade = "B"
        Case 70 To 80
            AssignGrade = "C"
        Case Is >= 60
            AssignGrade = "D"
        Case Else
            AssignGrade = "F"
    End Select
End Function
Sub test()
   MsgBox AssignGrade(99)
End Sub