Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Toggle Interior.ColorIndex using mouse clicks

Posted on 2013-10-22
2
Medium Priority
?
237 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 (Microsoft MVP / EE MVE) earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

926 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