Undo Macro

  • Thread starter Thread starter Broxterman
  • Start date Start date
B

Broxterman

Is there a simple way to undo a macro? I have a rather complex macr
that deletes empty lines on two pages and I would like to use anothe
macro bring me back to the original data before the lines were removed
Just curious if there is a simple way to do it or if I need to writ
another macro
 
You need to write your own undo procedure. Running a macro
clears Excel's own undo buffer.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Hello,

You could copy your two worksheets within the same
workbook by clicking a button. Then run your code to
remove empty lines. If you then decide to reset the
worksheets you can call the copied sheets back and give
them the original names. This code example assumes your
two worksheets are next to each other.

I hope this helps, DB

Sub SaveData()
Dim iSht1 As Integer, iSht2 As Integer
Dim iCount As Integer

'*******************************
'Identify the sheets to copy
'*******************************
iSht1 = ActiveSheet.Index
iSht2 = iSht1 + 1

'***************************************
'Copy the sheets before deleting rows
'and return to the first sheet to delete
'rows
'***************************************
iCount = Sheets.Count
ActiveSheet.Copy after:=Sheets(iCount)
iCount = iCount + 1
Sheets(iSht2).Copy after:=Sheets(iCount)

Sheets(iSht1).Activate

End Sub

Sub RestoreData()
Dim sSht1 As String, sSht2 As String
Dim iShtLoc As Integer

Application.DisplayAlerts = False
'*******************************************
'Replace the current sheet with the original
'sheet in the same sheet order.
'*******************************************
iShtLoc = ActiveSheet.Index
sSht1 = ActiveSheet.Name
sSht2 = Sheets(ActiveSheet.Index + 1).Name

'*************************
'Delete the changed sheets
'*************************
Sheets(sSht1).Delete
Sheets(sSht2).Delete

'**********************************
'reset the sheet names and position
'**********************************
With Sheets(Sheets.Count - 1)
.Name = sSht1
.Move after:=Sheets(iShtLoc - 1)
End With
With Sheets(Sheets.Count)
.Name = sSht2
.Move after:=Sheets(iShtLoc)
End With

'*************************************
'reset the display alerts and activate
'the original sheet
'*************************************
Application.DisplayAlerts = True
Sheets(sSht1).Activate

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Call Paste function with Undo 2
undo a macro 1
UNDO DELEING ROW AND POPUP WARNING MESSAGE 5
Undo VB macro action 1
Macro code to clipboard 2
Undo approach 3
UNDO 2
Macro to compare datas 3

Back
Top