ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   summing up cells up to the first unused cell encountered (https://www.excelbanter.com/excel-programming/424640-re-summing-up-cells-up-first-unused-cell-encountered.html)

mtzc

summing up cells up to the first unused cell encountered
 
Hi,

What would be the VBA code to sum up cells up to the first unused cell
encountered? For example, if cells A10-A12 have numbers, I want cell
A13 to sum from cell A12 up to the first unused cell (in this case
A9). I cannot just sum up the entire column A because there is data in
cells A1-A8 that I don't want to sum.

Thanks very much!

curlydave

summing up cells up to the first unused cell encountered
 
I suppose this would work, there will be problems though if the next
time you run the code the the last sum wasn't cleared so this code
will clear it first.

Dim s As Range

Set s = Range("A65536").End(xlUp)
s.ClearContents 'clears the last entry in Column A

Range("A9").End(xlDown).Offset(2, 0) = _
Application.Sum(Range(Range("A9"), Range("A9").End(xlDown)))


mtzc

summing up cells up to the first unused cell encountered
 
Thanks CurlyDave! Unfortunately, everytime I run the data, the data
group range I want to sum would start in different rows (depending on
how many rows the data group above it has), so is there any way to
tell the code to search bottom-up? Thanks again.

On Feb 24, 8:58*pm, CurlyDave wrote:
I suppose this would work, *there will be problems though if the next
time you run the code the the last sum wasn't cleared so this code
will clear it first.

Dim s As Range

Set s = Range("A65536").End(xlUp)
s.ClearContents 'clears the last entry in Column A

Range("A9").End(xlDown).Offset(2, 0) = _
Application.Sum(Range(Range("A9"), Range("A9").End(xlDown)))



curlydave

summing up cells up to the first unused cell encountered
 
Try This

Dim a As Range, s As Range, r As Range
Set a = Range("A1").End(xlDown).Offset(1, 0)
Set s = Range("A65536").End(xlUp)
s.ClearContents 'clears the last entry in Column A should be
the previous formula
Set r = Range("A65536").End(xlUp)
r.Offset(2, 0) = _
Application.Sum(Range(a, r))

mtzc

summing up cells up to the first unused cell encountered
 
Thanks so much Dave!


All times are GMT +1. The time now is 01:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com