Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of cell with mouse click in Excel

Tags:

excel

vba

I am trying to create a sheet where our employees can click on a cell to highlight it notating they are working of the task, and then click it again when they are finished with it, and click it a 3rd time if they need to clear the highlight. So far I have come up with the below, which works except that I have to click another cell and come back to the same one again or it will try to edit the cell. I Just want 1 click color change, another click same cell color change 2, another click same cell color change 3. Is there any way to do this?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)    
  'If the target cell is clear
     If Target.Interior.ColorIndex = xlNone Then

        'Then change the background to the specified color
        Target.Interior.ColorIndex = 6

        'But if the target cell is already the specified color
        ElseIf Target.Interior.ColorIndex = 6 Then

        'Then change the background to the specified color
        Target.Interior.ColorIndex = 3

        'But if the target cell is already the specified color
        ElseIf Target.Interior.ColorIndex = 3 Then

        'Then clear the background color
        Target.Interior.ColorIndex = xlNone

    End If    
End Sub
like image 562
user3159079 Avatar asked Jan 04 '14 00:01

user3159079


People also ask

How do you highlight a cell in Excel for clicking?

Double-click the cell, and then drag across the contents of the cell that you want to select. Click the cell, and then drag across the contents of the cell that you want to select in the formula bar.

How do I change the color of a row in Excel when a cell is clicked or selected?

In the Rule Description field, enter the formula: =OR(CELL(“col”)=COLUMN(),CELL(“row”)=ROW()) Click on the Format button and specify the formatting (the color in which you want the row/column highlighted). Click OK.

How do I change the color of a clicked button in Excel?

In the Properties window, (1) click on Alphabetic and (2) select ForeColor. After that, (3) click on the arrow to the right to display the color options, and (4) choose a color from the Palette (or System).


1 Answers

Add a BeforeDoubleClick event with this code in the same sheet:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Cancel = True
    Worksheet_SelectionChange Target

End Sub
like image 108
tigeravatar Avatar answered Sep 30 '22 05:09

tigeravatar