Solved

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

Posted on 2011-02-13
8
502 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
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 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

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 This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

912 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

17 Experts available now in Live!

Get 1:1 Help Now