Solved

Toggle Interior.ColorIndex using mouse clicks

Posted on 2013-10-22
2
192 Views
Last Modified: 2013-10-22
I have found the following code that changes the colour of a cell when clicked to Red

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then Target.Interior.ColorIndex = 3
End Sub

This is fine, but what happens if I click the wrong cell?

What code would do the following:

Click a single cell
Check the cell has [No Fill]
If [No Fill] then fill [Red]
If [Red] then reset to [No Fill]

I think I have the logic of what I want to do, but not the knowledge of the code required.
Which is where you experts come in.

Thanks for your time

Neil
0
Comment
Question by:NELMO
2 Comments
 
LVL 50

Accepted Solution

by:
teylyn earned 500 total points
Comment Utility
Hello Neil,

try this:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Count = 1 Then
    If Target.Interior.ColorIndex = 3 Then
        Target.Interior.ColorIndex = 0
    Else
        If Target.Interior.ColorIndex = -4142 Then
            Target.Interior.ColorIndex = 3
        End If
    End If
End If

End Sub

Open in new window

What it does:
If you select a cell and it has no fill, it will be filled with red. If a selected cell already has a colour fill other than red, the fill colour will not be changed. If the cell has a red fill, the red fill will be removed.

Copy the code. Right-click the worksheet tab, select "View Code" and paste the code into the code window.

cheers, teylyn
0
 

Author Closing Comment

by:NELMO
Comment Utility
Excellent Teylyn

that is exactly what I want.

Thanks

Neil
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 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

11 Experts available now in Live!

Get 1:1 Help Now