Clicking a cell so it changes colour, click it again and it changes back to the first colour, and so on.

mtaylor

Board Regular
Joined
May 1, 2013
Messages
73
Platform
  1. Windows
  2. MacOS
I'm trying to create a teaching resource for use in the classroom.

Basically I want to click on a cell and for that cell to change colour, click it again and it changes back to the first colour (almost like an undo)

Any help would be appreciated, my knowledge of coding is fairly basic. Apologies to the Americans also for my spelling of colour!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi.
Put code inside (ThisWorkbook)
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)Static OldRange As Range
On Error Resume Next
Target.Interior.ColorIndex = 8 ' yellow - change as needed
OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target


End Sub
 
Upvote 0
Welcome to the MrExcel board!


Basically I want to click on a cell and for that cell to change colour
To what colour?

If you click on, say, C3 we obviously need to record its colour and change its colour to the answer to my first question.
Might you then click on several other cells before clicking back on C3 and expecting it to go back to its original colour?
 
Upvote 0
Right click sheet tab. Click view code. Paste this, and test.

Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Target.Interior.Color = vbRed Then Target.Interior.Color = vbRed Else Target.Interior.Color = vbWhite
End Sub
 
Upvote 0
Right click sheet tab. Click view code. Paste this, and test.

Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Target.Interior.Color = vbRed Then Target.Interior.Color = vbRed Else Target.Interior.Color = vbWhite
End Sub

Looks good, if I didn't want the background to turn white though, rather an undo button.

Also is it possible to apply it to individual cells? For example clicking D4 turns it green, clicking C4 turns it red?

Appreciate the help folks!
 
Upvote 0
.. if I didn't want the background to turn white though, rather an undo button.
Which is partly why I asked the question I did. :)


Also is it possible to apply it to individual cells? For example clicking D4 turns it green, clicking C4 turns it red?
Give us more detail about what you have and what you are trying to achieve.

For example (really just a rewording of my original question) if D4 is clicked and it turns green, how long do we have to remember what colour D4 was before it was clicked? What happens if D4 is not clicked again until several other cells have also been clicked?
What happens if D4 is not clicked again until tomorrow, after the workbook has been saved, closed and re-opened? Do we still need to know what colour it originally was before it was turned green the day before?
What happens if D4 is never clicked again? Does it just stay green forever?
 
Upvote 0
Which is partly why I asked the question I did. :)


Give us more detail about what you have and what you are trying to achieve.

For example (really just a rewording of my original question) if D4 is clicked and it turns green, how long do we have to remember what colour D4 was before it was clicked? What happens if D4 is not clicked again until several other cells have also been clicked?
What happens if D4 is not clicked again until tomorrow, after the workbook has been saved, closed and re-opened? Do we still need to know what colour it originally was before it was turned green the day before?
What happens if D4 is never clicked again? Does it just stay green forever?


This forum is grand - thanks for the input everyone!

Ok...So as it stands each cell I click on changes to red, if I click back it changes to white. That's fine for what I want at the moment.
What I would really like though is to be able to assign different colours to different cells. For example clicking on A2 changes from dark green to light green and so on. If clicking on A3 it changes from dark yellow to light yellow.

Not sure if it's even possible to be honest but thanks again for the help so far.
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,892
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top