Sum the Variable Range

  • Thread starter Thread starter Sheikh Saadi
  • Start date Start date
S

Sheikh Saadi

Hi,
I want to sum the values of a column through VBA code. The problem is; I can
sum the value if the Range is static in the column, but if that is a variable
range, which is my case, my code is useless there.
Can anybody help me to sum up the values present in variable range in
column’s first empty cell?


Thanks,


sarfraz1
 
Try something like this:

firstempty = Range("D2").End(xlDown).Row
Range("D" & firstempty).Formula = "=SUM(D2:D" & firstempty - 1 & ")"

Regards,
Stefi

„Sheikh Saadi†ezt írta:
 
Try this:
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
..Cells(lastrow, "A").FormulaR1C1 _
= "=sum(r2c:r[-1]c)"

Notice, this sums elements in ColumnA, starting in Row2 and continuing down
through the last Row of the ColumnA


Ryan---
 
Thanks a lot both of you (Stefi and ryguy)… it helps me a lot…

--
Sheikh Saadi


ryguy7272 said:
Try this:
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(lastrow, "A").FormulaR1C1 _
= "=sum(r2c:r[-1]c)"

Notice, this sums elements in ColumnA, starting in Row2 and continuing down
through the last Row of the ColumnA


Ryan---

--
RyGuy


Stefi said:
Try something like this:

firstempty = Range("D2").End(xlDown).Row
Range("D" & firstempty).Formula = "=SUM(D2:D" & firstempty - 1 & ")"

Regards,
Stefi

„Sheikh Saadi†ezt írta:
 
You are welcome! Thanks for the feedback!
Stefi

„Sheikh Saadi†ezt írta:
Thanks a lot both of you (Stefi and ryguy)… it helps me a lot…

--
Sheikh Saadi


ryguy7272 said:
Try this:
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(lastrow, "A").FormulaR1C1 _
= "=sum(r2c:r[-1]c)"

Notice, this sums elements in ColumnA, starting in Row2 and continuing down
through the last Row of the ColumnA


Ryan---

--
RyGuy


Stefi said:
Try something like this:

firstempty = Range("D2").End(xlDown).Row
Range("D" & firstempty).Formula = "=SUM(D2:D" & firstempty - 1 & ")"

Regards,
Stefi

„Sheikh Saadi†ezt írta:

Hi,
I want to sum the values of a column through VBA code. The problem is; I can
sum the value if the Range is static in the column, but if that is a variable
range, which is my case, my code is useless there.
Can anybody help me to sum up the values present in variable range in
column’s first empty cell?


Thanks,


sarfraz1
 

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