Consulting

Results 1 to 5 of 5

Thread: Case Select Statement

  1. #1

    Case Select Statement

    Hi,

    I need a case statement with multiple(well only 2) conditions being met.
    Is there a way to do this. Here is what i've got:

    [vba]Case Is <= Cells(13, i).Value, Is > Cells(12, i).Value[/vba]

    The program will assume this is true if either the first OR the second equation is true.

    I need this case to be selected if both (AND) conditions are met.

    I tried replacing the comma with: AND, &, +. None of them works. Can this be done?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You need an IF/And statement and the operators need to follow the range....

    can you post an example file?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's an example macro using case with and, etc:
    [VBA]Sub Using_Case()
    ' Dimension the variable.
    Dim x As Integer
    ' Place a value in x.
    x = Int(Rnd * 100)
    ' Display the value of x.
    MsgBox "The value of x is " & x & "."
    ' Start the Select Case structure.
    Select Case x
    ' Test to see if x less than or equal to 10.
    Case Is <= 10
    ' Display a message box.
    MsgBox "X is <=10"
    ' Test to see if x less than or equal to 40 and greater than 10.
    Case 11 To 40
    MsgBox "X is <=40 and > 10"
    ' Test to see if x less than or equal to 70 and greater than 40.
    Case 41 To 70
    MsgBox "X is <=70 and > 40"
    ' Test to see if x less than or equal to 100 and greater than 70.
    Case 71 To 100
    MsgBox "X is <= 100 and > 70"
    ' If none of the above tests returned true.
    Case Else
    MsgBox "X does not fall within the range"
    End Select
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Hi Lucas,

    Thanks for help but this won't work as my conditions are based on cells which contains formula and numbers will change on a regular basis.

    I ended up building a messy if,elseif statement.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can refer to cells
    [VBA]
    Sub test()
    Select Case Range("A1")
    Case Is < Range("H5")
    MsgBox "Small"
    Case Range("H5") To Range("J5")
    MsgBox "Medium"
    Case Is > Range("J5")
    MsgBox "Large"
    End Select
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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