+ Reply to Thread
Results 1 to 4 of 4

Case If with multiple conditions

Hybrid View

PennStan Case If with multiple... 08-21-2014, 10:33 AM
ranman256 Re: Case If with multiple... 08-21-2014, 10:43 AM
sweep Re: Case If with multiple... 08-21-2014, 10:46 AM
PennStan Re: Case If with multiple... 08-21-2014, 12:52 PM
  1. #1
    Registered User
    Join Date
    09-12-2012
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 365
    Posts
    10

    Case If with multiple conditions

    I have not been able to find how to use the "Select Case" to follow my intended logic. I could use general IF statements, but I'm at the point where I just want to know how to use the select case for this. I want to set a variable based on the value of another variable. Below is my code that is not working correctly. On the second Case If, I want both conditions to be met. I tried using AND and TO between the two tests, but neither is accepted by VBA. As written, VBA behaves as though there is an "OR" between the two test conditions. (X could be any real number).

    Select Case X
    Case if < 100: Scale = 1
    Case if >= 100 < 1000: Scale = 10
    Case if >= 1000: Scale = 100
    End Select

    If X = 1025.26, the second Case If is true because X>=100. If I reverse the order of the Case If's, putting the >=1000 first, the same happens if X = 26.03. The second Case If is again true because X<1000.

    Is there a way to check multiple conditions using the Select Case? Thanks.

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,189

    Re: Case If with multiple conditions

    1. you dont need the IF
    2. you can use True for other checks.

    Select Case true
    Case x< 100
       Scale = 1
    Case  x>= 100 and  x < 1000 
       Scale = 10
    Case x >= 1000
        Scale = 100
    End Select

  3. #3
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Case If with multiple conditions

    Select Case X
    Case is <100 Scale = 1
    Case 100 to 1000: Scale = 10
    Case is >= 1000: Scale = 100
    
    End Select
    Last edited by sweep; 08-21-2014 at 10:50 AM.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  4. #4
    Registered User
    Join Date
    09-12-2012
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 365
    Posts
    10

    Re: Case If with multiple conditions

    OK, both worked. Ranman256's method is clear enough. Sweep's method, I see the "is" is left off the second "case" line. Never caught that in my references. Looking back, I see that is syntax.

    Thank you, both!

    8/24/14 - just realized I typed my example code wrong. Meant to say I was using Case IS, not Case If.
    Last edited by PennStan; 08-24-2014 at 08:53 AM. Reason: Correction to original post

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Case Statement with Multiple Conditions
    By ezrizer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-12-2014, 09:13 AM
  2. Using Case Select On Range With Multiple Conditions?
    By alulla in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-03-2013, 02:50 PM
  3. More than 3 formatting conditions - select case?
    By HelenW in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 01-08-2009, 04:58 AM
  4. Select case / case is, multiple arguments
    By Werner Rohrmoser in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-03-2006, 07:00 AM
  5. Case Statement using a Range of Conditions
    By Andrew Bird via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2005, 12:06 PM

Tags for this Thread

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