如果值更改,更改单元格颜色

我想编写一些VBA来改变单元格的颜色,如果用户改变到一个特定的工作表上不同的值。

用户每个月都会对同一张表进行更改。 用户将会改变某些字段。 没有什么变化可能是因为它可能是任何东西的清单。 我们已经要求用户突出显示他们是否对Excel工作表中的单元格进行更改。 但是我想写一个macros,它会自动检测。 但是,如果他们犯了一个错误,并把单元格恢复到它的原始值(打开文件的那一点),那么不需要高亮显示。

如果值更改,我有这个代码来更改单元格的颜色

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

但是,如果数值变回原始值,我该如何变回没有颜色?

提前谢谢了。

这里是你可以使用的东西:

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim rngCell As Range Set rngCell = Sheets(3).Cells(Target.Row, Target.Column) If IsEmpty(rngCell) Then rngCell = Target Target.Interior.Color = RGB(181, 244, 0) Else If rngCell = Target Then Target.Interior.Color = RGB(120, 120, 120) End If End If End Sub 

它将值设置为第三张,一旦创build,然后检查是否更改。 IsEmpty(rngCell)是检查。

编辑:关于格式的问题

如果你想,尝试实现以下某个地方:

 Private Sub CopyFromAtoB(rngA As Range, rngB As Range) rngB.Value = rngA.Value rngA.Copy rngB.PasteSpecial (xlPasteFormats) Application.CutCopyMode = False End Sub 

但是,要小心,因为如果你改变这个值,你可能会进入一个无限循环的地方。

不使用第二个电子表格的想法不是很好,你需要一些比较。 您可以将值保存在公共List或类似的VBA中,但一旦电子表格closures或VBA代码被破坏,您将失去一切。 而这是痛苦的。 所以这不是我所推荐的。

如果你想使它非常专业地使用一个SQL数据库 ,这将把你的解决scheme带到另一个层面。

正如@ YowE3Kbuild议的那样 – 你可以制作一个文件的副本并用它来比较。

将此代码添加到ThisWorkbook模块:

 Option Explicit Public tmpWrkBk As Workbook Private Sub Workbook_Open() Dim FSO As Object, TmpFolder As Object Dim tmpFileName As String Set FSO = CreateObject("Scripting.FileSystemObject") Set TmpFolder = FSO.GetSpecialFolder(2) 'Set reference to the temp folder. tmpFileName = FSO.GetBaseName(ThisWorkbook.Name) & Format(Now(), "dd-mmm-yy hh-mm-ss") 'Save this file as a temporary file. ThisWorkbook.SaveCopyAs TmpFolder & Application.PathSeparator & tmpFileName & ".xlsm" 'Open and hide the temp workbook. Application.EnableEvents = False Set tmpWrkBk = Workbooks.Open(Filename:=TmpFolder & Application.PathSeparator & tmpFileName & ".xlsm", _ UpdateLinks:=False, ReadOnly:=True) Workbooks(tmpFileName & ".xlsm").Windows(1).Visible = False Application.EnableEvents = True Set FSO = Nothing End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim tmpTarget As Range If Not tmpWrkBk Is Nothing Then Application.EnableEvents = False 'Set reference to same cell in temp workbook and compare values. Set tmpTarget = tmpWrkBk.Worksheets(Target.Parent.Name).Range(Target.Address) If Target.Value <> tmpTarget Then 'Value is different, so change the colour. Target.Interior.Color = RGB(181, 244, 0) Else 'Value is the same so change the formatting back again. tmpTarget.Copy Target.PasteSpecial Paste:=xlPasteFormats End If Application.EnableEvents = True End If End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim tmpFileName As String 'Close and delete the temp file before closing. If Not tmpWrkBk Is Nothing Then tmpFileName = tmpWrkBk.FullName Application.EnableEvents = False tmpWrkBk.Close savechanges:=False Application.EnableEvents = True Application.DisplayAlerts = False Kill tmpFileName Application.DisplayAlerts = True End If End Sub 

编辑:你会注意到我打开和closures临时文件时,已经把Application.EnableEvents – 这将停止Workbook_OpenWorkbook_Close事件发射临时文件(这将导致某种无限循环)。