Solved

Macro Question

Posted on 2013-12-01
12
271 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
  • 7
  • 4
12 Comments
 
LVL 46

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 46

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 46

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 46

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 46

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 46

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 46

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

856 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