Solved

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

Posted on 2011-02-13
8
503 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

770 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