+ Reply to Thread
Results 1 to 3 of 3

.ColorIndex: 3rd color by clicking cell?

  1. #1
    Registered User
    Join Date
    06-14-2010
    Location
    Berlin, Germany
    MS-Off Ver
    Excel 2003
    Posts
    1

    .ColorIndex: 3rd color by clicking cell?

    Hey!

    following problem: I want to change the background-color of a cell by just clicking it. I managed that by using the following code:

    Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim myRange As Range
    Set myRange = Application.Union(Range("A1:A3"), Range("B1:B2"))
    If Intersect(Target, myRange) Is Nothing Then Exit Sub

    If Target.Interior.ColorIndex = 1 Then
    With Selection.Interior
    .ColorIndex = 3
    End With
    Exit Sub
    End If

    With Selection.Interior
    .ColorIndex = 1
    End With

    End Sub


    So, when I click a cell within the range, it's color changes to black, 2nd click: from black to red, 3rd click: black again and so on...But, what do I do when I wanna have a third color (3rd click)? e.g. the loop would be "black, red, blue, black, ..." instead of "black, red, black, red..."?
    Any1 can help me?

  2. #2
    Registered User
    Join Date
    04-14-2010
    Location
    NZ
    MS-Off Ver
    2007,2010
    Posts
    86

    Re: .ColorIndex: 3rd color by clicking cell?

    simplist solution i can come up with - changing your if statements

    Try:
    Please Login or Register  to view this content.
    Edit: Alternatively, a case statement (which would allow more colours with less code)

    Please Login or Register  to view this content.
    Last edited by Deamo; 06-15-2010 at 12:03 AM.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: .ColorIndex: 3rd color by clicking cell?

    Durban,

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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