ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Changing Cell Color by clicking on a cell (https://www.excelbanter.com/excel-programming/395494-changing-cell-color-clicking-cell.html)

JEB

Changing Cell Color by clicking on a cell
 
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

Trevor Shuttleworth

Changing Cell Color by clicking on a cell
 
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


"JEB" wrote in message
...
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




JEB

Changing Cell Color by clicking on a cell
 
Thank you. It worked as expected.

"Trevor Shuttleworth" wrote:

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


"JEB" wrote in message
...
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






All times are GMT +1. The time now is 04:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com