+ Reply to Thread
Results 1 to 5 of 5

Storing the Interior.ColorIndex number in a variable

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Post Storing the Interior.ColorIndex number in a variable

    Dear all,

    I have a macro that would change:
    • White cell to Red
    • Red cell to White
    • Any other coloured cell to Red


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
             
    Select Case Target.Interior.ColorIndex
        Case Is = xlNone: Target.Interior.ColorIndex = 3                       'if white, then colour red
        Case Else: Select Case Target.Interior.ColorIndex
                        Case Is = 3: Target.Interior.ColorIndex = xlNone       'if red, then colour white
                        Case Else: Target.Interior.ColorIndex = 3                 'if other colours besides red, colour red
            
                    End Select
    End Select
        
    Cancel = True
        
    End Sub
    However, I would like to store the original colour of the cell, such that on the 1st double click, the cell becomes Red. On the 2nd double click, the cell changes back to the original colour.

    Does anybody know how to achieve this? Any ideas will be greatly appreciated. Thanks!
    Last edited by walkingaway; 07-29-2015 at 01:40 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Storing the Interior.ColorIndex number in a variable

    Like so;

    Option Explicit
    Public OldColor As Long
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        OldColor = Target.Interior.ColorIndex
    End Sub
    
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
             
    Select Case Target.Interior.ColorIndex
        Case Is = xlNone
            Target.Interior.ColorIndex = 3                          'if white, then colour red
        Case Else
            Select Case Target.Interior.ColorIndex
                Case 3
                    Target.Interior.ColorIndex = OldColor           'if red, then reset colour 
                Case Else
                    Target.Interior.ColorIndex = 3                  'if other colours besides red, colour red
            End Select
    End Select
        
    Cancel = True
        
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Storing the Interior.ColorIndex number in a variable

    Dear Jerry,

    Thanks for your help. The code works great. There however is a slight problem. After a cell (A1) has been coloured red via the macro, and I single click on another cell (B1), I am now unable to un-colour the cell (A1) by double-clicking on it. I believe this is due to the SelectionChange event being utilized here. Is there any way to bypass the SelectionChange event and build the macro solely in the Worksheet_BeforeDoubleClick event?

    Thank you (:

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Storing the Interior.ColorIndex number in a variable

    Once you move on to another cell, it won't matter which event you're using, I don't think, they will all lose track of your previous colors unless you store ALL the colors of ALL the cells somewhere else for reference. The SelectionChange at least lets you color a cell red and immediately change it back.

  5. #5
    Registered User
    Join Date
    06-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Storing the Interior.ColorIndex number in a variable

    Noted with thanks Jerry (: I will make do with this for now. Greatly appreciate your help.

+ 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. Interior.ColorIndex error ?
    By stuartgb100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-29-2015, 05:19 PM
  2. UDF with Interior.colorindex
    By mp3lab in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2011, 05:48 PM
  3. Using .Interior.ColorIndex with Conditions
    By vincentws in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2008, 11:29 AM
  4. Interior ColorIndex by range
    By kathemius in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2007, 04:44 PM
  5. Selection.Interior.ColorIndex
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-08-2007, 07:28 PM
  6. [SOLVED] problem with interior.colorindex
    By Peter Rooney in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2006, 08:35 AM
  7. Use of Interior.ColorIndex
    By liquidhot in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2005, 02:22 PM

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