Solved

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

Posted on 2010-11-12
7
289 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 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

12 Experts available now in Live!

Get 1:1 Help Now