Solved

Excel Macro to insert multiple lines

Posted on 2011-09-23
5
224 Views
Last Modified: 2012-05-12
Why does the undo button grey out after this macro is run.  I would like to have the user be able to click undo if they put the lines in the wrong part.

Sub Macro3()
'
' Macro3 Macro
' Macro recorded 9/23/2011 by Administrator
'

'
    ActiveCell.Rows("1:5").EntireRow.Select
    Selection.Insert Shift:=xlDown
    Sheets("Sheet2").Select
    Selection.Copy
    Sheets("Sheet1").Select
    ActiveCell.Select
    ActiveSheet.Paste
End Sub

Open in new window

0
Comment
Question by:WolfManBN25
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 36588804
Excel clears the undo stack whenever a macro is run.

You can define your own undo subroutine that can be run from the undo button.  In your specific example it would appear as follows:
 
Sub Macro3()
'
' Macro3 Macro
' Macro recorded 9/23/2011 by Administrator
    ActiveCell.Rows("1:5").EntireRow.Select
    Selection.Insert Shift:=xlDown
    Sheets("Sheet2").Select
    Selection.Copy
    Sheets("Sheet1").Select
    ActiveCell.Select
    ActiveSheet.Paste
    Application.OnUndo "Undo Insert", "Undo_Macro3" 'add this line to macro to call undo routine
End Sub

Sub Undo_Macro3()
    Range(Selection, Selection.Offset(4, 0)).Select
    Selection.EntireRow.Delete
End Sub

Open in new window

0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36588812
Write a public sub that reverses the macro you just ran.

Add a line to the current macro that calls the public sub:

Application.OnUndo "Undo something", "UnDoSomething"

This will enable the Undo button to run the public sub you wrote (the reverse of your macro).
0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36588814
Oops, sorry, didn't see the first response before submitting.  :)
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 36599372
Caveat:
I should note that the undo function will only work if run immediately after running the insert macro (Macro3).  If another cell/sheet is selected, the Undo function will act on that range instead of the original range.  

Also, if another edit is made, it adds that to the undo stack, eliminating the customized undo subroutine.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 36968914
If this problem has been resolved, please close the question.  thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now