VBA sum - doesn't work with only one value.
Hi there, please can anyone help. Have some VBA calculating a sum from
values in column E. Got the code from this google group, and have just
about worked out how it works. However, I don't know how to adapt it
so that it does a sum even if there is only one value to sum. I
pressume it is something to do with how the range is calculated. This
is what I've got.
lr = Cells(Rows.Count, "c").End(xlUp).Row
e1 = Cells(1, "c").End(xlDown).Row
Do Until e1 = lr
e2 = Cells(e1, "c").End(xlDown).Row
Set myrange = Range(Cells(e1, "e"), Cells(e2, "e"))
Cells(e1 - 1, "c") = Application.sum(myrange)
e1 = Cells(e2, "c").End(xlDown).Row
Loop
this is what the data looks like after the macro has been run.
A B C D E
0 b333333 141.88 20/06/2006
1 b333333 125001 1 28.49
1 b333333 125003 1 37.89
1 b333333 125005 1 75.50
0 b222222 108.12 20/06/2006
1 b222222 125007 1 88.34
0 B444444 20/06/2006
1 B444444 125009 1 19.78
1 B444444 039001 1 2.77
I think it should calculate from the bottom value until there is a
blankcell and then offset the total into column C in the row above, but
it falls over if there is a single value to calculate. It has also
missed the bottom value off this calculation, but all works perfectly
if there is more than one value to sum.
I would be gratefull for any suggestions. Thanks in advance, BevJ
|