Solved

Allow only one cell PER ROW to change color when dbl-clicked

Posted on 2010-11-12
7
290 Views
Last Modified: 2012-05-10
Hi, Experts!
I use the code below to toggle a cell's background color and font color when double-clicked.
However, I need to allow only one cell per row to have it's background and font color changed.
Is there any way to check for this?
The behavior would be:
Cell I6 is double-clicked:  color changes to blue/yellow.
Cell J6 is double-clicked:  color changes to blue/yellow, and cell I6 returns to no-color/black.
Cell I7 is double-clicked:  color changes to blue/yellow.
Cell I8 is double-clicked:  color changes to blue/yellow.
Cell K8 is double-clicked:  color changes to blue/yellow, and cell I8 returns to no-color/black.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

   Dim rngCheck As Range

   Dim lngColourIndex As Long

   lngColourIndex = 5

   Set rngCheck = Range("I6:M35")

   If Intersect(Target, rngCheck) Is Nothing Then Exit Sub

   If Target.Interior.ColorIndex = lngColourIndex Then

      Target.Interior.ColorIndex = xlColorIndexNone

      Target.Font.Color = vbBlack

   Else

      Target.Interior.ColorIndex = lngColourIndex

      Target.Font.Color = vbYellow

   End If

   Cancel = True

End Sub

Open in new window

0
Comment
Question by:OGSan
  • 4
  • 3
7 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34126025
Use this instead:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim rngCheck As Range
   Dim lngColourIndex As Long
   lngColourIndex = 5
   Set rngCheck = Range("I6:M35")
   If Intersect(Target, rngCheck) Is Nothing Then Exit Sub
   If Target.Interior.ColorIndex = lngColourIndex Then
      Target.Interior.ColorIndex = xlColorIndexNone
      Target.Font.Color = vbBlack
   Else
      Target.EntireRow.Interior.ColorIndex = xlColorIndexNone
      Target.EntireRow.Font.Color = vbBlack
      Target.Interior.ColorIndex = lngColourIndex
      Target.Font.Color = vbYellow
   End If
   Cancel = True
End Sub

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34126028
All I did was add a line to change the entire row to no color/black before setting the blue/yellow.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34126034
Or if you only want it to affect columns I through M use this instead.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim rngCheck As Range
   Dim lngColourIndex As Long
   lngColourIndex = 5
   Set rngCheck = Range("I6:M35")
   If Intersect(Target, rngCheck) Is Nothing Then Exit Sub
   If Target.Interior.ColorIndex = lngColourIndex Then
      Target.Interior.ColorIndex = xlColorIndexNone
      Target.Font.Color = vbBlack
   Else
      Range(Range("I" & Target.Row), Range("M" & Target.Row)).Interior.ColorIndex = xlColorIndexNone
      Range(Range("I" & Target.Row), Range("M" & Target.Row)).Font.Color = vbBlack
      Target.Interior.ColorIndex = lngColourIndex
      Target.Font.Color = vbYellow
   End If
   Cancel = True
End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:OGSan
ID: 34126040
Thanks, Tommy.  Is it possible to limit it to just the range defined?  The problem is that there is some cell formatting in the other columns.  THANKS!  Almost there...!
0
 
LVL 1

Author Closing Comment

by:OGSan
ID: 34126058
Outstanding - you actually posted this solution before I was able to reply to your initial solution.  FANTASTIC!  I really appreciate the quick reply, Tommy.  This really, really helps me out of a jam.  Thank you SO very much.
Jeff (aka OGSan)
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34126065
Yeah, I realized you might want it that way when I was testing it.
0
 
LVL 1

Author Comment

by:OGSan
ID: 34126069
Yes - you read my mind quite literally!  Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now