Copy Down - But not to the ends of the Earth!

zsnow_girl

New Member
Joined
Aug 18, 2008
Messages
5
Hi,

I'm having problems with macros. I'm creating huge files by copying down a formula within the macro to the end of the known universe. (Then I have to go into each file and delete it all by hand which often crashes my PC)

ActiveCell.FormulaR1C1 = "=RC[-3]-RC[-1]"
Range("F1:G1").Select
Selection.AutoFill Destination:=Range("F:G")
Range("F:G").Select

The macro runs through files of differing sizes. So how do I say I only want it to copy to the bottom of the table?

This probably has a simple solution. I just can't find it!

Please help,
Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi zsnow_girl,

The way you've written/recorded your macro makes it hard to follow, but see if my following suggestion helps (I've added some notes to hopefully make it easier to follow):

Code:
Sub Macro1()

    'Declare variable.
    Dim lngLastRow As Long
    
    'Set variable _
    Assumes the last row can be found from Column F - _
    change as required.
    lngLastRow = Cells(Rows.Count, "F").End(xlUp).Row
        
    'Enter the formula 'C1-E1' in cell F1 and copy _
    down to the row as defined by the _
    'lngLastRow' variable.
    Range("F1:F" & lngLastRow).Formula = "=C1-E1"

End Sub

HTH

Robert
 
Upvote 0
Why not name ranges?? Then you copy only what you want and put it where you want!

For this code, name a range "YourData". In fact, make that the cell where your formula resides. Now, name the range to where you wish to place the copied formula "RangeName". Run the macro. There ya go! (To name a range, simply select the cell or cells, then go to the space above cell A1. It will have the designation of the upper leftmost cell in your range. Select that space, the cell name will jump to the left. Now, type in your desired name for the range, such as "SnowGirl" or "Sno_Grl" or whatever you like. Confirm it with the Enter key.)

Code:
Sub CopyMe()
    Application.Goto "YourData"
    Selection.Copy
    Application.Goto "RangeName"
    ActiveSheet.Paste
    Application.Goto "YourData"
    Application.CutCopyMode = False
End Sub

Hope you can use this...
Regards,
XLXRider
 
Upvote 0
Hi,
Thanks for the tip - hopefully I can improve most of my macros with this.

My code (or at least some of it's) ended up looking like this. and it works!
Thanks again.

Dim lngLastRow As Long

lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row

'Calculate depth of base of peat
Range("F1").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*(R1C8/2)"

'Calculate OD of base of peat
Range("G1").Select
ActiveCell.FormulaR1C1 = "=RC[-3]-RC[-1]"
Range("F1:G1").Select
Selection.AutoFill Destination:=Range("F1:G" & lngLastRow)
Range("F:G").Select
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top