VBA代码突出显示已修改的单元格并在退出前清除突出显示

时间:2014-08-08 18:27:16

标签: excel vba excel-vba

我需要能够突出显示已修改的单元格,然后在关闭工作表之前,删除突出显示。通常,将要更改的单元格始终位于一列中。我一直在阅读开发人员参考资料并整天在vba中玩游戏。尽管我付出了最大的努力,但我似乎无法想象这一点。

我有一串代码突出显示任何目标细胞:

Target.Interior.Color = RGB(181, 244, 0)

我正在考虑创建一个记录目标单元格的数组,并在BeforeClose事件上,根据数组值清除格式。这甚至可能吗?

提前致谢!

3 个答案:

答案 0 :(得分:0)

在工作表代码区域中包含以下事件宏:

Private Sub Worksheet_Change(ByVal Target As Range)
    Target.Interior.Color = RGB(181, 244, 0)
End Sub

Private Sub Worksheet_Deactivate()
    Cells.Interior.ColorIndex = xlNone
End Sub

答案 1 :(得分:0)

Public workrange As Range

 Private Sub Worksheet_Change(ByVal Target As Range)
        ThisWorkbook.Names.Add Name:="MyRangeName", RefersTo:="Sheet1!$A$1"

        Set workrange = Range("MyRangeName")

        Target.Interior.Color = RGB(181, 244, 0)

        Set workrange = Union(workrange, Target.Address)

 End Sub

 Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim cell As Variant

        For Each cell In workrange
            If cell.Interior.Color <> RGB(0, 0, 0) Then
               cell.Interior.Color = RGB(0, 0, 0)
            End If
        Next

  End Sub
我相信

应该做你想做的事。

答案 2 :(得分:0)

我已经测试了以下代码,它在我身边起作用:

Private Sub Worksheet_Change(ByVal Target As Range) 'Add this to each required worksheet

Target.Interior.Color = RGB(181, 244, 0)

If HighlightedCells = "" Then
    HighlightedCells = Target.Address
Else
    HighlightedCells = HighlightedCells & "," & Target.Address
End If

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean) 'For the Workbook. Alter and repeat the line below for each required worksheet

ThisWorkbook.Worksheets("Sheet1").Range(HighlightedCells).Style = "Normal"

End Sub

Global HighlightedCells As String 'Add this to a new module