Solved

Excel Macro to insert multiple lines

Posted on 2011-09-23
5
230 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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