vba 在 Excel 中单击鼠标更改单元格的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20915335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change color of cell with mouse click in Excel
提问by user3159079
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?
我正在尝试创建一个工作表,我们的员工可以在其中单击一个单元格以突出显示它,表明他们正在执行任务,然后在完成任务后再次单击它,如果他们需要清除该任务,则第三次单击它强调。到目前为止,我已经提出了以下内容,除了我必须单击另一个单元格并再次返回相同的单元格,否则它将尝试编辑该单元格。我只想要 1 单击颜色更改,再次单击相同的单元格颜色更改 2,再次单击相同的单元格颜色更改 3。有什么办法可以做到这一点?
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
回答by tigeravatar
Add a BeforeDoubleClick event with this code in the same sheet:
在同一工作表中使用以下代码添加 BeforeDoubleClick 事件:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Worksheet_SelectionChange Target
End Sub