Copying formula to the bottom of a range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to create a macro that copies a formula in a column down to the
bottom of a range.

For example, I have data in columns A - L. In column M, I want to create a
formula and copy it down to the bottom of my data. The only problem is my
data is dynamic. One day it might end on row 1038, another day it might end
on row 1152.

Anyone have VBA code that can help me do this?
 
set rng = range(cells(1,"L"),cells(rows.count,"L").End(xlup))
rng.offset(0,1).Formula = "=Sum(A1:C1)"

make the formula relative to the first cell in your range and use absolute
and relative addressing as appropriate.

if you already have it in M1 for example, then it would be
set rng = range(cells(1,"L"),cells(rows.count,"L").End(xlup))
rng.offset(0,1).Formula = Range("M1").formula
 
thanks Tom, that worked like a charm.

Tom Ogilvy said:
set rng = range(cells(1,"L"),cells(rows.count,"L").End(xlup))
rng.offset(0,1).Formula = "=Sum(A1:C1)"

make the formula relative to the first cell in your range and use absolute
and relative addressing as appropriate.

if you already have it in M1 for example, then it would be
set rng = range(cells(1,"L"),cells(rows.count,"L").End(xlup))
rng.offset(0,1).Formula = Range("M1").formula
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top