PC Review


Reply
Thread Tools Rate Thread

Add sum to dynamic range in Macro

 
 
Rick
Guest
Posts: n/a
 
      23rd Jul 2008
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

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      23rd Jul 2008
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
>

 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      24th Jul 2008
Thanks Joel.

So for each section total do I just put this code where each section is
created?

Thanks again in advance

"Joel" wrote:

> 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
> >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      24th Jul 2008
I based this code on your original code that was using the ACTIVECELL. There
are better way of doing this if you have multiple sections of code on the
same worksheet.

It seem you have header rows seperated by one blank row and the rows of data
followed by at least one blank row. If there is some pattern to your
worksheet of a clear way of identifying the heders rows one macro can be
writen to transverse the entire worksheet.

You could have something as simple as this

Header Row
one blank row
Data cells
one or more blank rows

Header Row
one blank row
Data cells
one or more blank rows

repeat down the worksheet

"Rick" wrote:

> Thanks Joel.
>
> So for each section total do I just put this code where each section is
> created?
>
> Thanks again in advance
>
> "Joel" wrote:
>
> > 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
> > >

 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      24th Jul 2008
Joel,

I have just inserted this into the macro and it worked great except when it
copies the formula across the columns they all have the first column's
reference rather than the column they are in.

How do I fix this?

Thanks
Rick


"Joel" wrote:

> I based this code on your original code that was using the ACTIVECELL. There
> are better way of doing this if you have multiple sections of code on the
> same worksheet.
>
> It seem you have header rows seperated by one blank row and the rows of data
> followed by at least one blank row. If there is some pattern to your
> worksheet of a clear way of identifying the heders rows one macro can be
> writen to transverse the entire worksheet.
>
> You could have something as simple as this
>
> Header Row
> one blank row
> Data cells
> one or more blank rows
>
> Header Row
> one blank row
> Data cells
> one or more blank rows
>
> repeat down the worksheet
>
> "Rick" wrote:
>
> > Thanks Joel.
> >
> > So for each section total do I just put this code where each section is
> > created?
> >
> > Thanks again in advance
> >
> > "Joel" wrote:
> >
> > > 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
> > > >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      24th Jul 2008
The line below is corrected. Using Address returns $A$1. adding
columnAbosolute returns A$1.

TotalCell.Formula = "=SUM(" & _
FirstdataCell.Address(columnAbsolute:=False) & ":" & _
LastdataCell.Address(columnAbsolute:=False) & ")"

"Rick" wrote:

> Joel,
>
> I have just inserted this into the macro and it worked great except when it
> copies the formula across the columns they all have the first column's
> reference rather than the column they are in.
>
> How do I fix this?
>
> Thanks
> Rick
>
>
> "Joel" wrote:
>
> > I based this code on your original code that was using the ACTIVECELL. There
> > are better way of doing this if you have multiple sections of code on the
> > same worksheet.
> >
> > It seem you have header rows seperated by one blank row and the rows of data
> > followed by at least one blank row. If there is some pattern to your
> > worksheet of a clear way of identifying the heders rows one macro can be
> > writen to transverse the entire worksheet.
> >
> > You could have something as simple as this
> >
> > Header Row
> > one blank row
> > Data cells
> > one or more blank rows
> >
> > Header Row
> > one blank row
> > Data cells
> > one or more blank rows
> >
> > repeat down the worksheet
> >
> > "Rick" wrote:
> >
> > > Thanks Joel.
> > >
> > > So for each section total do I just put this code where each section is
> > > created?
> > >
> > > Thanks again in advance
> > >
> > > "Joel" wrote:
> > >
> > > > 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
> > > > >

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      24th Jul 2008
just another way:

LastdataCell.Address(,0)

--


Gary


"Joel" <(E-Mail Removed)> wrote in message
news:87F8C060-0F16-4FA8-B803-(E-Mail Removed)...
> The line below is corrected. Using Address returns $A$1. adding
> columnAbosolute returns A$1.
>
> TotalCell.Formula = "=SUM(" & _
> FirstdataCell.Address(columnAbsolute:=False) & ":" & _
> LastdataCell.Address(columnAbsolute:=False) & ")"
>
> "Rick" wrote:
>
>> Joel,
>>
>> I have just inserted this into the macro and it worked great except when it
>> copies the formula across the columns they all have the first column's
>> reference rather than the column they are in.
>>
>> How do I fix this?
>>
>> Thanks
>> Rick
>>
>>
>> "Joel" wrote:
>>
>> > I based this code on your original code that was using the ACTIVECELL.
>> > There
>> > are better way of doing this if you have multiple sections of code on the
>> > same worksheet.
>> >
>> > It seem you have header rows seperated by one blank row and the rows of
>> > data
>> > followed by at least one blank row. If there is some pattern to your
>> > worksheet of a clear way of identifying the heders rows one macro can be
>> > writen to transverse the entire worksheet.
>> >
>> > You could have something as simple as this
>> >
>> > Header Row
>> > one blank row
>> > Data cells
>> > one or more blank rows
>> >
>> > Header Row
>> > one blank row
>> > Data cells
>> > one or more blank rows
>> >
>> > repeat down the worksheet
>> >
>> > "Rick" wrote:
>> >
>> > > Thanks Joel.
>> > >
>> > > So for each section total do I just put this code where each section is
>> > > created?
>> > >
>> > > Thanks again in advance
>> > >
>> > > "Joel" wrote:
>> > >
>> > > > 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
>> > > > >



 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      24th Jul 2008
Gary, how many people know what (,0) really means. It doesn't cost anything
to add the columnAbsolute:=False and it makes the code easier to maintain. I
guess you never took any real courses in programming.

"Gary Keramidas" wrote:

> just another way:
>
> LastdataCell.Address(,0)
>
> --
>
>
> Gary
>
>
> "Joel" <(E-Mail Removed)> wrote in message
> news:87F8C060-0F16-4FA8-B803-(E-Mail Removed)...
> > The line below is corrected. Using Address returns $A$1. adding
> > columnAbosolute returns A$1.
> >
> > TotalCell.Formula = "=SUM(" & _
> > FirstdataCell.Address(columnAbsolute:=False) & ":" & _
> > LastdataCell.Address(columnAbsolute:=False) & ")"
> >
> > "Rick" wrote:
> >
> >> Joel,
> >>
> >> I have just inserted this into the macro and it worked great except when it
> >> copies the formula across the columns they all have the first column's
> >> reference rather than the column they are in.
> >>
> >> How do I fix this?
> >>
> >> Thanks
> >> Rick
> >>
> >>
> >> "Joel" wrote:
> >>
> >> > I based this code on your original code that was using the ACTIVECELL.
> >> > There
> >> > are better way of doing this if you have multiple sections of code on the
> >> > same worksheet.
> >> >
> >> > It seem you have header rows seperated by one blank row and the rows of
> >> > data
> >> > followed by at least one blank row. If there is some pattern to your
> >> > worksheet of a clear way of identifying the heders rows one macro can be
> >> > writen to transverse the entire worksheet.
> >> >
> >> > You could have something as simple as this
> >> >
> >> > Header Row
> >> > one blank row
> >> > Data cells
> >> > one or more blank rows
> >> >
> >> > Header Row
> >> > one blank row
> >> > Data cells
> >> > one or more blank rows
> >> >
> >> > repeat down the worksheet
> >> >
> >> > "Rick" wrote:
> >> >
> >> > > Thanks Joel.
> >> > >
> >> > > So for each section total do I just put this code where each section is
> >> > > created?
> >> > >
> >> > > Thanks again in advance
> >> > >
> >> > > "Joel" wrote:
> >> > >
> >> > > > 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
> >> > > > >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Range Name Macro Jonathan Cooper Microsoft Excel Misc 3 24th Mar 2008 09:04 PM
dynamic range in a macro MJKelly Microsoft Excel Programming 2 19th Nov 2007 11:48 PM
macro code to get dynamic range ashish128 Microsoft Excel Misc 3 15th Jun 2007 02:41 PM
Macro to print a dynamic range =?Utf-8?B?UnVzcw==?= Microsoft Excel Programming 3 13th Jan 2005 05:31 PM
Creating a name in a macro with a dynamic range Eric G. Microsoft Excel Discussion 1 8th Dec 2003 11:45 PM


Features
 

Advertising
 

Newsgroups
 


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