Solved

Macro Question

Posted on 2013-12-01
12
223 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 45

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 45

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 45

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 45

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

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 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel Error per WinZip Add-In 9 25
Auto Operation of MS Excel 2013 2 33
EXCEL Addin problem 7 26
Excel Macro - Lookup-Highlight 4 25
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;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

759 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