Solved

Toggle Interior.ColorIndex using mouse clicks

Posted on 2013-10-22
2
217 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:
Ingeborg Hawighorst earned 500 total points
ID: 39590650
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
ID: 39590947
Excellent Teylyn

that is exactly what I want.

Thanks

Neil
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

856 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