vbaexcel

Using AND in VBA excel with Select case


I am trying to write a simple function that give me band 1 if the value is zero Band 2 if it's between 0 to 150k Band 3 if between 150k to 500k and so on

I only get 1 if the value is zero and everything else is 2, even if I enter 10 million

Please help

Function Band(ARR)

    Select Case ARR

        Case Is = 0
            Band = 1

        Case Is >= 0.1 And ARR <= 150000
            Band = 2

        Case Is >= 150000.01 And ARR <= 500000
            Band = 3

        Case Is >= 500000.01 And ARR <= 1500000
            Band = 4

        Case Is >= 1500000.01
            Band = 5

        Case Else
            Band = 6

    End Select

End Function

Solution

  • You could use the "To" keyword to specify a range. See here for more information.

    Function Band(arr)
    Select Case arr
      Case 0
        Band = 1
      Case 0.01 To 150000
        Band = 2
      Case 150000.01 To 500000
        Band = 3
      Case 500000.01 To 1500000
        Band = 4
      Case Is >= 1500000.01
        Band = 5
      Case Else
        Band = 6
      End Select
    End Function