[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Macro Question

Posted on 2013-12-01
12
Medium Priority
?
291 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 49

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 49

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

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 49

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 49

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 49

Assisted Solution

by:Martin Liss
Martin Liss earned 400 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 1600 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 49

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

656 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