Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to sum columns in all sheets in a workbook

I need to write a macro to sum the same columns (ie.. G:O) in every worksheet
within a workbook. I need to add it to a macro that I already have which
splits sheet 1 into other sheets using the information in column A. I have
that macro working as well as using an auto fit macro for all sheets, but now
I need to total the columns in each sheet that was created. I have a macro
that works when it is one sheet but I don't know how to do it for all
sheets. Also, the columns in each sheet will end in different rows.

--
Thank you, Jodie
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to sum columns in all sheets in a workbook


Something like this. You have to skip the original shet that you split
and I included row 1 which you also may want to change to row 2. I'm
using column A to determine the last row and putting the total and the
next row after the last row.

Sub test()

firstCol = ActiveSheet.Columns("G").Column
LastCol = ActiveSheet.Columns("O").Column


For Each Sht In Sheets
With Sht
'find last row using column A
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
SumRow = LastRow + 1
'Add formula to worksheet to add first column
Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))
.Cells(SumRow, firstCol).Formula = _
"=Sum(" & SumRange.Address(ColumnAbsolute:=False) & ")"

'copy the formula across all the columns
.Cells(SumRow, firstCol).Copy _
Destination:=.Range(.Cells(SumRow, firstCol), .Cells(SumRow,
LastCol))
'Put Total in column a
.Range("A" & SumRow) = "Total"
End With
Next Sht

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=148941

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to sum columns in all sheets in a workbook

Thank you Joel. I will give it a try and let you know how it goes.
--
Thank you, Jodie


"joel" wrote:


Something like this. You have to skip the original shet that you split
and I included row 1 which you also may want to change to row 2. I'm
using column A to determine the last row and putting the total and the
next row after the last row.

Sub test()

firstCol = ActiveSheet.Columns("G").Column
LastCol = ActiveSheet.Columns("O").Column


For Each Sht In Sheets
With Sht
'find last row using column A
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
SumRow = LastRow + 1
'Add formula to worksheet to add first column
Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))
.Cells(SumRow, firstCol).Formula = _
"=Sum(" & SumRange.Address(ColumnAbsolute:=False) & ")"

'copy the formula across all the columns
.Cells(SumRow, firstCol).Copy _
Destination:=.Range(.Cells(SumRow, firstCol), .Cells(SumRow,
LastCol))
'Put Total in column a
.Range("A" & SumRow) = "Total"
End With
Next Sht

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=148941

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to sum columns in all sheets in a workbook

Hi Joel, I tried it and I am getting an error.

Compile error
Syntax error

It occurs at Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))

I should tell you that I am adding this to a module that I already have that
creates the sheets. What I have that already creates the sheets may creat a
sheet with only one row which is the header. There are 2 sheets created with
no records except the header. They are named NULL and PFSPLANID. I tried
deleting those sheets and rerunning, but I get the same error. Can you
please help me figure out what I am doing wrong?
--
Thank you, Jodie


"Jodie" wrote:

Thank you Joel. I will give it a try and let you know how it goes.
--
Thank you, Jodie


"joel" wrote:


Something like this. You have to skip the original shet that you split
and I included row 1 which you also may want to change to row 2. I'm
using column A to determine the last row and putting the total and the
next row after the last row.

Sub test()

firstCol = ActiveSheet.Columns("G").Column
LastCol = ActiveSheet.Columns("O").Column


For Each Sht In Sheets
With Sht
'find last row using column A
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
SumRow = LastRow + 1
'Add formula to worksheet to add first column
Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))
.Cells(SumRow, firstCol).Formula = _
"=Sum(" & SumRange.Address(ColumnAbsolute:=False) & ")"

'copy the formula across all the columns
.Cells(SumRow, firstCol).Copy _
Destination:=.Range(.Cells(SumRow, firstCol), .Cells(SumRow,
LastCol))
'Put Total in column a
.Range("A" & SumRow) = "Total"
End With
Next Sht

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=148941

.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to sum columns in all sheets in a workbook

Hi Joel, I figured out the problem. Thank you very much for your help.
--
Thank you, Jodie


"Jodie" wrote:

Hi Joel, I tried it and I am getting an error.

Compile error
Syntax error

It occurs at Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))

I should tell you that I am adding this to a module that I already have that
creates the sheets. What I have that already creates the sheets may creat a
sheet with only one row which is the header. There are 2 sheets created with
no records except the header. They are named NULL and PFSPLANID. I tried
deleting those sheets and rerunning, but I get the same error. Can you
please help me figure out what I am doing wrong?
--
Thank you, Jodie


"Jodie" wrote:

Thank you Joel. I will give it a try and let you know how it goes.
--
Thank you, Jodie


"joel" wrote:


Something like this. You have to skip the original shet that you split
and I included row 1 which you also may want to change to row 2. I'm
using column A to determine the last row and putting the total and the
next row after the last row.

Sub test()

firstCol = ActiveSheet.Columns("G").Column
LastCol = ActiveSheet.Columns("O").Column


For Each Sht In Sheets
With Sht
'find last row using column A
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
SumRow = LastRow + 1
'Add formula to worksheet to add first column
Set SumRange = .Range(.Cells(1, firstCol), .Cells(LastRow,
firstCol))
.Cells(SumRow, firstCol).Formula = _
"=Sum(" & SumRange.Address(ColumnAbsolute:=False) & ")"

'copy the formula across all the columns
.Cells(SumRow, firstCol).Copy _
Destination:=.Range(.Cells(SumRow, firstCol), .Cells(SumRow,
LastCol))
'Put Total in column a
.Range("A" & SumRow) = "Total"
End With
Next Sht

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=148941

.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
In need to link Columns between sheets of a workbook? SkyscraperCM Excel Discussion (Misc queries) 2 December 12th 08 12:49 AM
Resize columns for all sheets in a workbook Xluser@work[_2_] Excel Programming 1 May 16th 06 09:39 AM
macro to compile columns on multiple sheets simonsmith Excel Discussion (Misc queries) 2 May 9th 06 04:06 PM
Macro for filter on protected workbook that works for all sheets, no matter what sheets are named? StargateFanFromWork[_3_] Excel Programming 6 January 26th 06 06:31 PM
comparing 2 similar columns on seperate work sheets in 1 workbook Dan Excel Discussion (Misc queries) 4 September 20th 05 11:58 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"