2012-01-09 77 views
0

Excel 2003,我想通过点击鼠标来改变单元格的颜色。我已经完成了,我的问题是如何在单元格上单击鼠标后再次获得原始颜色。我希望能够将单元格的颜色从白色更改为绿色,并且每次单击鼠标时都会再次更改颜色。用鼠标单击改变单元格的颜色

+1

请发表您的当前代码 – brettdj 2012-01-10 02:09:42

回答

3

这是我在我的一张床单上使用的。基本上用户双击单元格以创建用户表单;并在提交更正时突出显示细胞黄色。如果他们犯了一个错误,他们双击删除高亮。你应该能够在下面的代码中找出你所需要的。每下面的评论

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

Application.EnableEvents = False 

    Dim TargRow As Variant 
    Dim TargCol As Variant 

    TargRow = Target.Row 
    TargCol = Target.Column 

    Header = 8 
    FirstCol = 0 
    LastCol = 13 
    CommentCol = 13 

    If TargRow > Header And TargCol > FirstCol And TargCol < LastCol Then 
     'If the cell is clear 
     If Target.Interior.ColorIndex = xlNone Then 
      Cancel = True 

      'Then change the background to yellow 
      Target.Interior.ColorIndex = 6 
      Corrections.Show 

      'Else if the cell background color is already yellow 
      ElseIf Target.Interior.ColorIndex = 6 Then 

      'Then clear the background 
      Target.Interior.ColorIndex = xlNone 
     End If 
    End If 

    'This is to prevent the cell from being edited when double-clicked 
    Cancel = True 

    Application.EnableEvents = True 
End Sub 

编辑的代码:

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 = 4 

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

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

    End If 

End Sub 
+0

感谢您的回复(以“这个我做了”暗示的),但我仍然有问题。我试图将这个应用于特定的单元格。我现在拥有如下: – 2012-01-09 21:43:50

+0

好的。我砍掉了上面的东西,并改变了放在哪里。该代码作为SelectionChange事件放置在工作表上。它只需要你点击一个单元格(而不是像上面那样双击);点击单元格将单元格变成绿色 - 单击单元格再次清除颜色。此外,细胞仍然可以编辑,不像我上面的原始答案。请参阅上面的代码编辑答案。 – Jon 2012-01-10 13:44:56

相关问题