+ Reply to Thread
Results 1 to 7 of 7

Setting ranges for comparison operators

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    64

    Setting ranges for comparison operators

    Hi everyone,

    Is it possible to set a range using comparison operators? ie. if a number lies within x and y then such and such happens.

    To be more precise:

    Function grade(maths As Double, english As Double, science As Double) As String
    
    If maths >= 70 And english >= 70 And science >= 70 Then
        grade = "Distinction"
    
    ElseIf maths >= 70 And english >= 50 And science >= 50 Then
        grade = "Merit"
    
    ElseIf maths < 50 Or english < 50 Or science < 50 Then
        grade = "Fail"
        
    End If
    End Function
    Focusing on the red text, I was hoping to set english and science so that they are also < 70 (lie between 50 - 69).

    Thanks for your time!
    Last edited by dems; 07-22-2009 at 10:14 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Setting ranges for comparison operators

    Not sure of the logic you're trying to apply. Maybe this:
    Function grade(maths As Double, english As Double, science As Double) As String
        Dim dMin As Double
        Dim dMax As Double
        
        dMin = WorksheetFunction.Min(maths, english, science)
        dMax = WorksheetFunction.Max(maths, english, science)
        
        If dMin >= 70 Then
            grade = "Distinction"
        ElseIf dMax >= 70 And dMin >= 50 Then
            grade = "Merit"
        ElseIf dMin < 50 Then
            grade = "Fail"
        Else
            grade = "???"
        End If
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Setting ranges for comparison operators

    hi Dems,

    I think you have given the answer to your own question in your example (once corrected by changing the red "70" to a "50")...?

    Here's a very slightly alternative approach to your function, using Long for the arguments which may be slightly faster for large data sets, because I think that Double is "overkill". Note that the order of the tests in the function means that the range of 50-69 is already "defined" within the second Case, because the arguments have failed the initial test (ie ">=70") but do pass the ">50" test.

    Function grade(maths As Long, english As Long, science As Long) As String
    'alternative #1
    Select Case True
        Case maths >= 70 And english >= 70 And science >= 70
            grade = "Distinction"
        Case maths >= 50 And english >= 50 And science >= 50
            grade = "Merit"
        Case maths < 50 Or english < 50 Or science < 50
            grade = "Fail"
    End Select
    
    'alternative #2
    'Using slightly different rules for the grading which _
    allows high grades in one subject to pull up the _
    overall mark (provided all subjects are >50).
    Dim TtlOfMarks As Long
    TtlOfMarks = maths + english + science
    Select Case True
        Case maths < 50 Or english < 50 Or science < 50
            grade = "Fail"
        Case TtlOfMarks >= 210 'ie 70x3
            grade = "Distinction"
        Case TtlOfMarks >= 150
            grade = "Merit"
    End Select
    End Function
    End Function
    If you need more help let us know

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Setting ranges for comparison operators

    Shg, you beat me to it again - good to see that I'm starting to think similarly to yourself though

    Although, I do like your use of the .min function because this would mean changes would only need to be made in the Function's arguments & the .min line of code if (or when) the function is modified to allow for more subjects.

    Rob

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Setting ranges for comparison operators

    I dunno, Rob, don't know what the OP is trying to do. Many roads lead to Rome.

  6. #6
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    64

    Re: Setting ranges for comparison operators

    Thanks gentlemen, you have been verty helpful! I will definately spend some time reading further into case statements and worksheetfunction.min/max, but both yield the result I was looking for, which was to analyse 3 test results and report a grade in relation to the test results. Once more, thank you.

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Setting ranges for comparison operators

    I'm pleased we could help - thanks for the feedback

    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1