Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel VBA – Select Case with comparison

I’ve got simple Select Case statement which doesn’t work as intended.

I want to do different actions depending on my value being greater or equal to 1 or being less than 1.

When myValue is declared as Double procedure doesn’t find a match (nothing is returned), when myValue is declared as Long it returns the first case "more than 1" – where this is incorrect as myValue is set to 0.5 …

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can somebody tell me what I’m missing?

Public Sub test()

    Dim myValue As Double
    
    myValue = 0.5

    
    Select Case myValue
    
        Case myValue >= 1
            Debug.Print "more than 1"
        Case myValue < 1
            Debug.Print "less than 1"
    
    End Select

End Sub

>Solution :

Use the correct syntax:

Public Sub test()

    Dim myValue As Double
    
    myValue = 0.5
    
    Select Case myValue
        Case Is >= 1
            Debug.Print "more than 1"
        Case Is < 1
            Debug.Print "less than 1"
    End Select

End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading