Solved

Excel Macro to insert multiple lines

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

757 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

19 Experts available now in Live!

Get 1:1 Help Now