Solved

Macro Question

Posted on 2013-12-01
12
277 Views
Last Modified: 2013-12-01
EE Pros,

I've been watching "how to record Macros" on YouTube and am improving my skills. However, I have a question for you;

I'm trying to record a simple Macro that inserts a set of blank lines and then moves the cursor to the next line and stops.  Then when the Macro is fired again, it inserts another set of blank lines.  Right now, what it does when I record it is it takes the line number (Row) and adds the lines correctly.  When I fire it again, it refers back to the LINE NUMBER and adds more lines where the original lines were added.  How do I get the Macro to recognize that I'm inserting lines and not relate it to a specific row when I record?

Thank you in advance,

B.
0
Comment
Question by:Bright01
[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
  • 7
  • 4
12 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688227
Please post the macro.
0
 

Author Comment

by:Bright01
ID: 39688253
Here is an example.  The first tab is without the Macro (what I want -- what I get).  Then the second tab is after I record the Macro and it relates to the actual lines; not the ability to "insert" the new lines.......

Thank you,

B.
Macro-Lesson.xlsm
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688270
Sorry but I'm not sure what you start with and what you want.

Do you start with this?

Question1
Question2
Question3
Question4

and do you want to end up with this?
Question1


Question2


Question3


Question4
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688273
If so then try this code which assumes that the "Questions" always start at row 8.

    Dim lngRow As Long
    With ActiveSheet
        For lngRow = .Range("E65536").End(xlUp).Row To 9 Step -1
            .Rows(lngRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            .Rows(lngRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next
    End With

Open in new window


BTW I would not use Ctrl+s as the shortcut since that's normally "save". Use Ctrl+Shift+s instead.
0
 

Author Comment

by:Bright01
ID: 39688291
Martin,

The reason I was posting the question is because I'm trying to learn how to "record macros".  The problem isn't really the code, it's how to record the Macro without recording the actual line references.  I'm simply trying to add two lines and advance the cursor to do the next two lines when I fire the macro.

B.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688308
The macro recorder records exactly what you do including the actual line references and there's no way to change that behavior.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688314
You could do something like this which always refers to the active cell.

Rows(ActiveCell.Row).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
0
 

Author Comment

by:Bright01
ID: 39688328
So Martin,

I'm recording a macro that says, insert 2 rows below the row I start recording on.  Then move the cursor down to the original row (now 2 rows down) and stop.  When I fire the Macro again, why wouldn't it simply "repeat" the command?  Instead it goes back to the original row number.

So if I want to insert a line after a line, when I record a macro, it will only identify the fact that I want the insert in that specific location or row number?  There is no way to record the fact that I want to insert a row below a row and then move the cursor to the original row (not row number)?

B.
0
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 100 total points
ID: 39688398
No there isn't. While the ability to record macros is very valuable, what you get is very primitive and often not the most efficient way to do the task. What I do, and I believe what most people do is, when needed, record the macro and then modify it to be more flexible and/or more efficient.
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 400 total points
ID: 39688472
In 2007 on the "Developer" tab, Just below the "Record Macro" button there is a button which says "Use relative references". This is probably what you are looking for.
0
 

Author Closing Comment

by:Bright01
ID: 39688489
Ssaqbh,

Tried it.  And it worked!  By using RR, it apparently recognizes what I'm trying to do without tagging the exact cell locations.  Thank you.

Martin,

I'll take your advise and only use record macros to begin building on what I want.  Unfortunately, I think it takes years to get as good as you guys in building the more complex macros out.

Thanks to both of you.

B.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39688592
In 2007 on the "Developer" tab, Just below the "Record Macro" button there is a button which says "Use relative references".
Never knew that!

Unfortunately, I think it takes years to get as good as you guys in building the more complex macros out.
It really doesn't and as they say, once you learn how to fish you can feed yourself forever.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

726 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