Changing Cell Color by clicking on a cell

G

Guest

I have a spreadsheet on which I want to change the background color of the
cell when the user clicks on it. Ex.

If (selection).interior.colorindex = 0 then
(selection).interior.colorindex = 4
else
(selection.interior.colorindex = 0
end if.

Basically I want to toggle the color between green and white for a give
range of cells. How do I do it?

Thank you all in advance.

JB
 
T

Trevor Shuttleworth

JB

This should give you the basic idea:

Public OldCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("A1:C8")) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If OldCell Is Nothing Then Set OldCell = Target
OldCell.Interior.ColorIndex = 0
Target.Interior.ColorIndex = 4
End Sub

The routine needs to be in the Sheet Class Module for the sheet where you
want to operate.

Right click on the sheet tab, View Code, and paste the code above.

This limits the range to A1 to C7 ... just amend to suit your requirements

Regards

Trevor
 
G

Guest

Thank you. It worked as expected.

Trevor Shuttleworth said:
JB

This should give you the basic idea:

Public OldCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("A1:C8")) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If OldCell Is Nothing Then Set OldCell = Target
OldCell.Interior.ColorIndex = 0
Target.Interior.ColorIndex = 4
End Sub

The routine needs to be in the Sheet Class Module for the sheet where you
want to operate.

Right click on the sheet tab, View Code, and paste the code above.

This limits the range to A1 to C7 ... just amend to suit your requirements

Regards

Trevor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top