View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
JW[_2_] JW[_2_] is offline
external usenet poster
 
Posts: 638
Default Programmatically add worksheet function

On May 21, 8:58*am, Office_Novice
wrote:
I haven't tried this before now and i am having a little trouble.

This is what i have:
Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C5000)"

And it works ok, But i wont always know the range. I need something like:

Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C" & LastRow)"

But i tried that and the function didn’t recognize my variable. Any help
would be great.


Your inserting a formula. Why are you using .Value? You should use
something like this:
Dim LastRow As Long
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Worksheets("Sheet2").Range("C1").Formula = _
"=SUM(C2:C" & LastRow & ")"