VBA code to undo previous VBA action

  • Thread starter Thread starter Preschool Mike
  • Start date Start date
P

Preschool Mike

I was wondering if code can be written to undo a previous vba action.
Specifically I have a range of cell that I clear with some vba code. Was
wondering if code can be written recall what was previsiouly cleared? I
found that I can do it with just one cell, but I'm working with a range of
cells and can't seem to get it to work.

Here's the code I use to clear my contents.

Sub ClearMasterAttendance()
Range("'Lunch and Attendance'!AD7:BH22").Select
Selection.ClearContents

End Sub

Now can anyone please help me with some code to undo this action.

Thanks,
 
Have ClearMasterAttendance first save the block of data in a working area and
then clear it. The Undo macro would just copy the saved area back.
 
Sorry, but what exactly do you mean by "working area"? Another cell range?
or declare it with a Dim statement? I tried several Dim statements Integer,
Long, Double, but none work. The only one I could get to work was Variant.
Is it ok to use Dim Undo As Variant? Do you forsee any problems with this
type of declaration?
 
Here is some sample coding. We are using a spare area to store a copy.

Sub ClearMasterAttendance()
Dim sh As Worksheet
Dim DataBlock As Range
Dim UndoBlock As Range
Set sh = Sheets("Lunch and Attendance")
Set DataBlock = sh.Range("AD7:BH22")
Set UndoBlock = sh.Range("ET1:FX16")
DataBlock.Copy UndoBlock
DataBlock.ClearContents
End Sub

Sub undo()
Dim sh As Worksheet
Dim DataBlock As Range
Dim UndoBlock As Range
Set sh = Sheets("Lunch and Attendance")
Set DataBlock = sh.Range("AD7:BH22")
Set UndoBlock = sh.Range("ET1:FX16")
UndoBlock.Copy DataBlock
End Sub

If ET1:FX16 is not usable, pick another spare area.
 
Try some things.

Post back with your specific questions.

Preschool said:
Thanks for the suggestion, but this is all very new to me and I don't know
how to adjust the code on his site for my situation.
 
Back
Top