Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Toggle Interior.ColorIndex using mouse clicks

Posted on 2013-10-22
2
Medium Priority
?
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
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…

722 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