Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,358
Default VBA Program to calculate Subtotals



I have tried different methods to achieve this (pivot tables etc) but I
don't get the required result in the right format.



I have a workbook with the first Sheet named "Totals". The rest of the
worksheets "Sheet2, Sheet3 ..etc" have a description in "Column A" and a
quantity in "Column B"



Example:-



"Sheet 2"

Col A ----- Col B

ITEM01 ----- 2

ITEM03 ----- 1

ITEM01 ----- 2

ITEM02 ----- 5

ITEM06 ----- 0

ITEM05 ----- 10



"Sheet 3"

ITEM01 ----- 1

ITEM02 ----- 1

ITEM05 ----- 2

ITEM04 ----- 2



I require a macro to compare the descriptions and add the quantities of each
description and show the summary on the totals page. The following needs to
be taken into account.

a) New worksheets might be added so the macro wants to look in every
worksheet apart from the first one "Totals"

b) If the description has a quantity of 0 it wants to be ignored and not
added to the Totals page.



When running the macro I would like to see the following results on the
Totals page.

"Totals"

ITEM01 ----- 5

ITEM02 ----- 6

ITEM03 ----- 1

ITEM04 ----- 2

ITEM05 ----- 12



Like I mentioned I have used pivot tables but the format in inappropriate
and new worksheets might be added, also I want this user friendly.

Has anyone done anything like this before, this macro would be very useful
to much appreciated.



Thanks

John








  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,886
Default VBA Program to calculate Subtotals

Hi John

Create a new Sheet called Summary
Leave Column A blank
In column B enter your list of Items, ITEM01, ITEM02 etc.
In column C, enter
=SUMPRODUCT(SUMIF(INDIRECT("'"&Snames&"'!A:A"),
C1,INDIRECT("'"&Snames&"'!B:B")))
and copy down as far as you have items listed.

Copy the following macro to the workbook

Sub ListSheets()
Dim ws As Worksheet, i As Long

For Each ws In Worksheets
If ws.Name < "Summary" Then
i = i + 1
Sheets("Summary").Cells(i, 1) = ws.Name
End If
ActiveWorkbook.Names.Add Name:="Snames", RefersToR1C1:= _
Sheets("Summary").Range(Cells(1, 1), Cells(i, 1))
Next

End Sub

This will create a list of sheet names which are then used as Snames in
the INDIRECT() portion of the formula as above.
Run the macro after you have added any sheets, and your formula will
adjust to give the correct totals.

--
Regards

Roger Govier


"John C" <johncAAATTlamberteng.com wrote in message
...


I have tried different methods to achieve this (pivot tables etc) but
I don't get the required result in the right format.



I have a workbook with the first Sheet named "Totals". The rest of the
worksheets "Sheet2, Sheet3 ..etc" have a description in "Column A" and
a quantity in "Column B"



Example:-



"Sheet 2"

Col A ----- Col B

ITEM01 ----- 2

ITEM03 ----- 1

ITEM01 ----- 2

ITEM02 ----- 5

ITEM06 ----- 0

ITEM05 ----- 10



"Sheet 3"

ITEM01 ----- 1

ITEM02 ----- 1

ITEM05 ----- 2

ITEM04 ----- 2



I require a macro to compare the descriptions and add the quantities
of each description and show the summary on the totals page. The
following needs to be taken into account.

a) New worksheets might be added so the macro wants to look in every
worksheet apart from the first one "Totals"

b) If the description has a quantity of 0 it wants to be ignored and
not added to the Totals page.



When running the macro I would like to see the following results on
the Totals page.

"Totals"

ITEM01 ----- 5

ITEM02 ----- 6

ITEM03 ----- 1

ITEM04 ----- 2

ITEM05 ----- 12



Like I mentioned I have used pivot tables but the format in
inappropriate and new worksheets might be added, also I want this user
friendly.

Has anyone done anything like this before, this macro would be very
useful to much appreciated.



Thanks

John










  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,358
Default VBA Program to calculate Subtotals

Hi Roger,

Thankyou for taking the time reply, I have followed your instructions but
get a "Run-time error `1004' " when stepping through the macro getting to
the line above next. Just running the macro displays a red cross with 400 in
it. I'm using Office 2000 I'm afraid.

Thanks again
John


"Roger Govier" wrote in message
...
Hi John

Create a new Sheet called Summary
Leave Column A blank
In column B enter your list of Items, ITEM01, ITEM02 etc.
In column C, enter
=SUMPRODUCT(SUMIF(INDIRECT("'"&Snames&"'!A:A"),
C1,INDIRECT("'"&Snames&"'!B:B")))
and copy down as far as you have items listed.

Copy the following macro to the workbook

Sub ListSheets()
Dim ws As Worksheet, i As Long

For Each ws In Worksheets
If ws.Name < "Summary" Then
i = i + 1
Sheets("Summary").Cells(i, 1) = ws.Name
End If
ActiveWorkbook.Names.Add Name:="Snames", RefersToR1C1:= _
Sheets("Summary").Range(Cells(1, 1), Cells(i, 1))
Next

End Sub

This will create a list of sheet names which are then used as Snames in
the INDIRECT() portion of the formula as above.
Run the macro after you have added any sheets, and your formula will
adjust to give the correct totals.

--
Regards

Roger Govier


"John C" <johncAAATTlamberteng.com wrote in message
...


I have tried different methods to achieve this (pivot tables etc) but I
don't get the required result in the right format.



I have a workbook with the first Sheet named "Totals". The rest of the
worksheets "Sheet2, Sheet3 ..etc" have a description in "Column A" and a
quantity in "Column B"



Example:-



"Sheet 2"

Col A ----- Col B

ITEM01 ----- 2

ITEM03 ----- 1

ITEM01 ----- 2

ITEM02 ----- 5

ITEM06 ----- 0

ITEM05 ----- 10



"Sheet 3"

ITEM01 ----- 1

ITEM02 ----- 1

ITEM05 ----- 2

ITEM04 ----- 2



I require a macro to compare the descriptions and add the quantities of
each description and show the summary on the totals page. The following
needs to be taken into account.

a) New worksheets might be added so the macro wants to look in every
worksheet apart from the first one "Totals"

b) If the description has a quantity of 0 it wants to be ignored and not
added to the Totals page.



When running the macro I would like to see the following results on the
Totals page.

"Totals"

ITEM01 ----- 5

ITEM02 ----- 6

ITEM03 ----- 1

ITEM04 ----- 2

ITEM05 ----- 12



Like I mentioned I have used pivot tables but the format in inappropriate
and new worksheets might be added, also I want this user friendly.

Has anyone done anything like this before, this macro would be very
useful to much appreciated.



Thanks

John












  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,886
Default VBA Program to calculate Subtotals

Hi John

Sloppy coding on my part I'm afraid.
I will run OK if Summary is the Active sheet when you run the macro.

I had stupidly put the creation of the Named range inside the loop,
causing it to be activated each time a new sheet name was added.
I have now moved it outside the loop, and ensured that Summary is
activated before creating the Named range so it doesn't matter what
sheet you are on when you run the macro

Sub ListSheets()
Dim ws As Worksheet, i As Long

For Each ws In Worksheets
If ws.Name < "Summary" Then
i = i + 1
Sheets("Summary").Cells(i, 1) = ws.Name
End If
Next
Sheets("Summary").Activate
ActiveWorkbook.Names.Add Name:="Snames", RefersToR1C1:= _
Sheets("Summary").Range(Cells(1, 1), Cells(i, 1))

End Sub

My apologies for such poor coding.

--
Regards

Roger Govier


"John C" <johncAAATTlamberteng.com wrote in message
...
Hi Roger,

Thankyou for taking the time reply, I have followed your instructions
but get a "Run-time error `1004' " when stepping through the macro
getting to the line above next. Just running the macro displays a red
cross with 400 in it. I'm using Office 2000 I'm afraid.

Thanks again
John


"Roger Govier" wrote in message
...
Hi John

Create a new Sheet called Summary
Leave Column A blank
In column B enter your list of Items, ITEM01, ITEM02 etc.
In column C, enter
=SUMPRODUCT(SUMIF(INDIRECT("'"&Snames&"'!A:A"),
C1,INDIRECT("'"&Snames&"'!B:B")))
and copy down as far as you have items listed.

Copy the following macro to the workbook

Sub ListSheets()
Dim ws As Worksheet, i As Long

For Each ws In Worksheets
If ws.Name < "Summary" Then
i = i + 1
Sheets("Summary").Cells(i, 1) = ws.Name
End If
ActiveWorkbook.Names.Add Name:="Snames", RefersToR1C1:= _
Sheets("Summary").Range(Cells(1, 1), Cells(i, 1))
Next

End Sub

This will create a list of sheet names which are then used as Snames
in the INDIRECT() portion of the formula as above.
Run the macro after you have added any sheets, and your formula will
adjust to give the correct totals.

--
Regards

Roger Govier


"John C" <johncAAATTlamberteng.com wrote in message
...


I have tried different methods to achieve this (pivot tables etc)
but I don't get the required result in the right format.



I have a workbook with the first Sheet named "Totals". The rest of
the worksheets "Sheet2, Sheet3 ..etc" have a description in "Column
A" and a quantity in "Column B"



Example:-



"Sheet 2"

Col A ----- Col B

ITEM01 ----- 2

ITEM03 ----- 1

ITEM01 ----- 2

ITEM02 ----- 5

ITEM06 ----- 0

ITEM05 ----- 10



"Sheet 3"

ITEM01 ----- 1

ITEM02 ----- 1

ITEM05 ----- 2

ITEM04 ----- 2



I require a macro to compare the descriptions and add the quantities
of each description and show the summary on the totals page. The
following needs to be taken into account.

a) New worksheets might be added so the macro wants to look in every
worksheet apart from the first one "Totals"

b) If the description has a quantity of 0 it wants to be ignored and
not added to the Totals page.



When running the macro I would like to see the following results on
the Totals page.

"Totals"

ITEM01 ----- 5

ITEM02 ----- 6

ITEM03 ----- 1

ITEM04 ----- 2

ITEM05 ----- 12



Like I mentioned I have used pivot tables but the format in
inappropriate and new worksheets might be added, also I want this
user friendly.

Has anyone done anything like this before, this macro would be very
useful to much appreciated.



Thanks

John














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
Subtotals problem: Excel 2003 (not, AFAIK, the nested subtotals bug) AndyCotgreave Excel Discussion (Misc queries) 3 October 24th 07 11:32 AM
Original subtotals should not be within nested subtotals in excel Mirage Excel Worksheet Functions 1 June 6th 07 01:37 AM
How to calculate percentage of values of subtotals Alicia Excel Discussion (Misc queries) 5 February 15th 06 02:04 PM
Program to calculate how much I can afford to spend? Looking to start over Excel Discussion (Misc queries) 3 November 30th 05 10:12 PM
Problem with nested subtotals, placing secondary subtotals BELOW . Dawn Cameron Excel Discussion (Misc queries) 1 June 3rd 05 10:13 PM


All times are GMT +1. The time now is 01:52 PM.

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

About Us

"It's about Microsoft Excel"