+ Reply to Thread
Results 1 to 35 of 35

More than 3 formatting conditions - select case?

Hybrid View

  1. #1
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this, change the condition ("A","B" etc) to suit

    Option Explicit
    
    'START OF CODE
    
    
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim intColour As Integer
    
        If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
            Select Case Target
                Case "A": intColour = 6
                Case "B": intColour = 3
                Case "C": intColour = 7
                Case "D": intColour = 18
                Case "E": intColour = 15
                Case "F": intColour = 42
                Case Else:    'do nothing
            End Select
            Target.Interior.ColorIndex = intColour
        End If
    End Sub
    To add this code to your worksheet, do the following:

    Copy the code that you want to use
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    This code should work for you

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim intColour As Integer
    
    Set rng = ActiveSheet.Range(Cells(2, 8), Cells(Rows.Count, 13).End(xlUp))
        
        If Not Intersect(Target, rng) Is Nothing Then
            Select Case Range("P2").Value
                Case 1: intColour = 6
                Case 2: intColour = 3
                Case 3: intColour = 7
                Case 4: intColour = 18
                Case 5: intColour = 15
                Case 6: intColour = 42
                Case Else:    'do nothing
            End Select
            Target.Interior.ColorIndex = intColour
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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