Solved

Excel VB for copying formulas from last row to the next row

Posted on 2011-02-13
8
501 Views
Last Modified: 2012-05-11
Hi all!  Another stupid newbie question.

I am attempting to copy the formulas from the last line to the next open line after that line in Excel 2007.  Below is the code but it keeps copying the formula a the starting cell B11 instead of the next open line.  I know the problem is the line of code

 Range("B11").Select

How do I change that code to copy from the last line to the next open line ("B11") was the line that was open when I recorded my macro.

Sub CopyNewPayDatesAddtlInfo()
'
' Macro14 Macro
'

'
    Range("B4").Select
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Range("B11").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("B4").Select
End Sub

Open in new window

0
Comment
Question by:wlwebb
  • 4
  • 2
8 Comments
 
LVL 20

Expert Comment

by:pari123
ID: 34883100
Hi wlwebb,
I think there is a lot of redundant lines in this code. can you please explain what you are trying to do here?

- Ardhendu
0
 
LVL 20

Assisted Solution

by:pari123
pari123 earned 500 total points
ID: 34883120
Here's one way to achieve what you are trying to do
Sub CopyNewPayDatesAddtlInfo()
' Macro14 Macro
    n = Cells(65536, "B").End(xlUp).Row
    Range("B" & n + 1).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.FillDown
End Sub

Open in new window

0
 

Author Comment

by:wlwebb
ID: 34883152
Pari
I know that there are easier ways to do what I am attempting.  Just not very good at it yet so you can only go with what you know.  

Anyway, in looking at your code I have a question.  In the line  (n = Cells(65536, "B").End(xlUp).Row) why put that line of code in?  What is the purpose of 65536?  What happens when the user reaches line 65537 of data?

Keep in mind, I am only wanting to copy the formula to one row, that row being the very next open line and to that line only.  Will you code accomplish that?  Or will it copy that formula all the way down to line 65536?
0
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

 
LVL 20

Accepted Solution

by:
pari123 earned 500 total points
ID: 34883169

1. the 65536 is the last row that exists in excel 2003.
2. so the code in line 3 is checking to find what is the last row that was used in Column B. basically, the code starts checking the cells backwards from 65536 till it reaches a cell that is not empty.
3. next the macro will copy the last line of data and paste it ONLY in the next row.

try the macro once and see if that works for you,

- Ardhendu
0
 

Author Closing Comment

by:wlwebb
ID: 34883266
Thanks for the input
0
 
LVL 20

Expert Comment

by:pari123
ID: 34883275
Hi wlwebb,

is there a reason for grading this as B? Please see the following help section on the grades to give.

http://www.experts-exchange.com/help.jsp#hs=29&hi=403

thanks,
Ardhendu

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34897030
Instead of 65536, you should get in the habit of using Rows.Count so it will be compatible across all versions.
Also, a B grade is supposed to be used if the experts didn't really get you the solution.
Since you didn't ask any further questions pari certainly assumed that you didn't need any more help. It does not appear to me that anything was lacking.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

758 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

22 Experts available now in Live!

Get 1:1 Help Now