Consulting

Results 1 to 16 of 16

Thread: Select Case

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location

    Select Case

    I have the following bit of code, what i want it to do is colour the cell according to the value, at the moment it will do the first one which is HOL, but not the rest of them, do i need to set it up as a select case, if so how do i do that.


    Range("d:d").Activate
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=""hol"""
    Selection.FormatConditions(1).Interior.ColorIndex = 39
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=""sick"""
    Selection.FormatConditions(1).Interior.ColorIndex = 4
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=""r"""
    Selection.FormatConditions(1).Interior.ColorIndex = 45

    Alao when that has been completed i want to look for certain cell values, when it finds them i want to insert an entire row.
    can that be done using the same select case.



    Thanks For any help

    Ian

  2. #2
    Mac Moderator VBAX Expert shades's Avatar
    Joined
    May 2004
    Location
    Kansas City, USA
    Posts
    638
    Location
    Something like this, you would have to adjust some, and define myRng, but this should get you started.


    Select Case myRng
                     Case "hol":
                         .Interior.ColorIndex = 39
                     Case "sick"
                         .Interior.ColorIndex = 4
                     Case "r":
                         .Interior.ColorIndex = 45
                     Case Else
                         .Interior.ColorIndex = 1
                 End Select

    Software: LibreOffice 3.3 on Mac OS X 10.6.5
    (retired Excel 2003 user, 3.28.2008 )
    Humanware: Older than dirt
    --------------------
    old, slow, and confused
    but at least I'm inconsistent!

    Rich

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    To run on all cells in column D with data in them:

    Sub test()
     Dim cl As Range
     For Each cl In ActiveSheet.Range("D1:D" & ActiveSheet.Range("D65536").End(xlUp).Row)
         Select Case cl.Value
             Case Is = "hol"
                 cl.Interior.ColorIndex = 39
             Case Is = "sick"
                 cl.Interior.ColorIndex = 4
             Case Is = "r"
                 cl.Interior.ColorIndex = 45
             Case Else
                 cl.Interior.ColorIndex = 1
         End Select
     Next cl
     End Sub
    You could also nest an If statement or another case either within the individual cases or before the Next Cl statement to insert a row. If you need help, just let us know the criteria.

    Also, you may want to consider either adding an Option Compare Text just after the Option Explicit statement or converting both the case to be tested (cl) to lowercase to avoid case sensitivity issues. (If a user enters Hol instead of hol, for example.)

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Iam,

    to insert the row as you have asked you may use this:

    Sub test() 
       Dim cl As Range 
       For Each cl In ActiveSheet.Range("D1:D" & ActiveSheet.Range("D65536").End(xlUp).Row) 
          Select Case cl.Value 
             Case Is = "hol" 
                cl.Interior.ColorIndex = 39
             Case Is = "sick" 
                cl.Interior.ColorIndex = 4 
             Case Is = "r" 
                cl.Interior.ColorIndex = 45 
             Case Else 
                cl.Interior.ColorIndex = 1 
          End Select
          'If you want to insert a row in all use this, else put it on where you want to insert a row
          cl.Select
          Selection.EntireRow.Insert
       Next cl 
    End Sub
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi,

    Paleo, there are some things you don't need to put, in lieu of performance/assumptions. You don't need to put the ActiveSheet, it's implied. And you don't need to select anything, you would already be iterating through the range object.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Ok, thanks. I will do this in my next posts.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  7. #7
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thanks for all the replys, I have just finished a 13 hour night shift and i am now going to bed, so when i get up i will try some of the suggestions

    Ian

  8. #8
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Ok I have now tried the codes supplied but what i get is a black interior color in col d:d


    Thanks


    Ian

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This line makes all the cells that do not match one of the criteria black.

    Case Else
    cl.Interior.ColorIndex = 1
    To leave them uncolored try this.

    Case Else
    cl.Interior.ColorIndex = xlNone

  10. #10
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thanks for that Jake, that now works.

    Just one last thing though, How do i get it to not be case sensitive, as sometimes they could be all in caps or lower case, as it only picks out the lower case at the moment.

    Thanks

    Ian

  11. #11
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi Ian,

    You could use the LCase function to convert the text being examined in the Select Case construct into all lowercase and then compare it to lowercase text. Alternatively, use the UCase function to compare it to uppercase text.

    Like this:
    Sub test()
    If LCase(Sheet1.Range("A1").Value) = "ian" Then
            MsgBox "Its Ian!"
        End If
        '
        'or
        '
        If UCase(Sheet1.Range("A1").Value) = "IAN" Then
            MsgBox "Its Ian!"
        End If
    End Sub
    
    HTH

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Edit: Didn't see Richie's post!! Oops! I guess I'll leave this up; maybe it'll help somebody.


    Use either UCase or LCase. If you kept all of your text lower case in your individual Case statements, use LCase. An example of each (w/ the code above) ...

    Using LCase:
    Sub test() 
        Dim cl As Range 
        For Each cl In ActiveSheet.Range("D1:D" & ActiveSheet.Range("D65536").End(xlUp).Row) 
            Select Case LCase(cl.Value) '<<-- This is where you set the case
            Case Is = "hol" 
                cl.Interior.ColorIndex = 39 
            Case Is = "sick" 
                cl.Interior.ColorIndex = 4 
            Case Is = "r"
                cl.Interior.ColorIndex = 45 
            Case Else 
                cl.Interior.ColorIndex = 1 
            End Select 
        Next cl 
    End Sub

    Using UCase:
    Sub test() 
        Dim cl As Range 
        For Each cl In ActiveSheet.Range("D1:D" & ActiveSheet.Range("D65536").End(xlUp).Row) 
            Select Case UCase(cl.Value) '<<-- This is where you set the case
            Case Is = "HOL" 
                cl.Interior.ColorIndex = 39 
            Case Is = "SICK" 
                cl.Interior.ColorIndex = 4 
            Case Is = "R"
                cl.Interior.ColorIndex = 45 
            Case Else 
                cl.Interior.ColorIndex = 1 
            End Select 
        Next cl 
    End Sub
    So basically, just force the capitalization of the value being checked, as it's basically a variable, and make your Case statements match.


    HTH

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To ignore case, use Option Compare Text at the start of your module.
    MD
    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'

  14. #14
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thank you for everybodys help here.
    Md that was what i was looking for, so if some one made the mistake of putting lowercase instead of upper it didn't matter.
    It now works a treat, apart from it now takes about 40 secs to run the macro where as i used to take 6-7 secs, is there anyway that would speed this up a bit.

    Thanks

    Ian

  15. #15
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    We can speed up the loop by limiting the iterations. Since we only want to change cells with text we can use Special Cells to narrow the range. If you have a lot of cells in the range that are blank, this should help a lot.

    Option Compare Text
     Option Explicit
     
     Sub test()
    Dim cl              As Range
     Dim Rng1            As Range
    Set Rng1 = Range("D1:D" & Range("D65536").End(xlUp).Row)
         Rng1.Interior.ColorIndex = xlNone
         Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants, 2)
         For Each cl In Rng1
             Select Case cl.Text
             Case Is = "HOL"
                 cl.Interior.ColorIndex = 39
             Case Is = "SICK"
                 cl.Interior.ColorIndex = 4
             Case Is = "R"
                 cl.Interior.ColorIndex = 45
             End Select
         Next cl
    End Sub

  16. #16
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thank you for that DRJ.

    It is now working perfect, infact it is faster than it was before, it now takes about 3-4 secs.

    Thank you to all who helped with this.
    I think i can now safely say that this thread is Solved.





    Ian

Posting Permissions

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