Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel Macro to insert multiple lines

Posted on 2011-09-23
5
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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!
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

618 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