Solved

Macro Question

Posted on 2013-12-01
12
243 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

21 Experts available now in Live!

Get 1:1 Help Now