View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Add sum to dynamic range in Macro

It is always better to use intermediate variables that are descriptive so you
can document the code easily.

Set HeaderCell = ActiveCell
Set FirstdataCell = HeaderCell.Offset(2, 0)
Set LastDataCell = FirstdataCell.End(xlDown)
LastDataCell.Resize(10, 14).ClearContents
LastDataCell.Resize(7, 14).Delete Shift:=xlUp

'reset Lastdatacell since it was deleted
Set LastDataCell = FirstdataCell.End(xlDown)
Set TotalCell = LastDataCell.Offset(2, 0)
TotalCell.Formula = "=SUM(" & FirstdataCell.Address & ":" & _
LastDataCell.Address & ")"
'copy formula across row
TotalCell.Copy _
Destination:=Range(TotalCell, TotalCell.Offset(0, 14))

"Rick" wrote:

I download a report into Excel and I have created a macro to reformat it so
it is easy to read.

I want to add totals at the bottom of each section break. The problem I have
is that I don't know how big each section is as it changes from month to
month. when I add the section breaks I use;

ActiveCell.Offset(2, 0).End(xlDown).Resize(10, 14).Select
Selection.ClearContents
ActiveCell.Resize(7, 14).Select
Selection.Delete Shift:=xlUp

Which takes the cells I don't want and deletes and clears them out leaving 3
blank lines.

There are several of these sections of various lengths and I want to add
total to the bottom of each of them, there are 6 columns in each section.

How do I get the formula to add all cells above up to the first blank cell?

Thanks in advance
Rick