Solved

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

Posted on 2011-02-13
8
504 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

829 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