PC Review


Reply
Thread Tools Rate Thread

How to add the same cell in multiple worksheets (with VBA)?

 
 
Damien McBain
Guest
Posts: n/a
 
      24th Jan 2008
Hi,
I need to add the same cells in multiple worksheets. I want the worksheets
to be selected dynamically using a range on another worksheet which contains
the names of the worksheets to sum. I don't want any formulas in the summary
worksheet - I want to populate the cells with values.

I'm thinking something like this to populate a Collection (I've never used
collections before):
--------------------
Dim Allsheets As New Collection
For Each branch In Worksheets("Tables").Range("BusAreaList")
Allsheets.Add Item:=branch
Next branch
--------------------
....but I don't know how to use the Items in the Collection to specify which
cells to sum (or even if I can!). Am I on the right track?
Any help appreciated.
TIA
Damien


 
Reply With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      24th Jan 2008
No need to use collection, you say you already have a list of sheets in the
range BusAreaList so use that.

Something like

Dim c as Range, myTotal as double
myTotal = 0
For each c in Range("BusAreaList")
myTotal = myTotal + Sheets(c.value).Range("A1")
Next c

The above adds the value in A1 on all sheets named in the range BusAreaList
to the variable myTotal. You might want to validate that the list sheet
names are valid.


--

Regards,
Nigel
(E-Mail Removed)



"Damien McBain" <(E-Mail Removed)> wrote in message
news:O67%(E-Mail Removed)...
> Hi,
> I need to add the same cells in multiple worksheets. I want the worksheets
> to be selected dynamically using a range on another worksheet which
> contains the names of the worksheets to sum. I don't want any formulas in
> the summary worksheet - I want to populate the cells with values.
>
> I'm thinking something like this to populate a Collection (I've never used
> collections before):
> --------------------
> Dim Allsheets As New Collection
> For Each branch In Worksheets("Tables").Range("BusAreaList")
> Allsheets.Add Item:=branch
> Next branch
> --------------------
> ...but I don't know how to use the Items in the Collection to specify
> which cells to sum (or even if I can!). Am I on the right track?
> Any help appreciated.
> TIA
> Damien
>


 
Reply With Quote
 
Damien McBain
Guest
Posts: n/a
 
      24th Jan 2008
Thanks for that Nigel

I was hoping not to have to do it like that because there are a couple of
hundred cells I need to resolve in a similar manner. It's going to make the
code very long and laborious (but maybe necessary).

I thought I might be able to use the same principal as the worksheet
function:
=Sum(Sheet2:Sheet20!B8)
.... but I don't know how to specify a multi sheet range like that in VBA
(given that the sheet names in my workbook are dynamic - but always listed
in the range "BusAreaList")

cheers,

Damien


"Nigel" <nigel-(E-Mail Removed)> wrote in message
news:728F418D-0410-40D4-BD05-(E-Mail Removed)...
> No need to use collection, you say you already have a list of sheets in
> the range BusAreaList so use that.
>
> Something like
>
> Dim c as Range, myTotal as double
> myTotal = 0
> For each c in Range("BusAreaList")
> myTotal = myTotal + Sheets(c.value).Range("A1")
> Next c
>
> The above adds the value in A1 on all sheets named in the range
> BusAreaList to the variable myTotal. You might want to validate that the
> list sheet names are valid.
>
>
> --
>
> Regards,
> Nigel
> (E-Mail Removed)
>
>
>
> "Damien McBain" <(E-Mail Removed)> wrote in message
> news:O67%(E-Mail Removed)...
>> Hi,
>> I need to add the same cells in multiple worksheets. I want the
>> worksheets to be selected dynamically using a range on another worksheet
>> which contains the names of the worksheets to sum. I don't want any
>> formulas in the summary worksheet - I want to populate the cells with
>> values.
>>
>> I'm thinking something like this to populate a Collection (I've never
>> used collections before):
>> --------------------
>> Dim Allsheets As New Collection
>> For Each branch In Worksheets("Tables").Range("BusAreaList")
>> Allsheets.Add Item:=branch
>> Next branch
>> --------------------
>> ...but I don't know how to use the Items in the Collection to specify
>> which cells to sum (or even if I can!). Am I on the right track?
>> Any help appreciated.
>> TIA
>> Damien
>>

>



 
Reply With Quote
 
Nigel
Guest
Posts: n/a
 
      24th Jan 2008
You can use the application worksheet function in VBA to replicate the
=Sum(Sheet2:Sheet20!B8) to get the sum from multiple sheets, but as you say
the sheets list is not contiguous nor is it fixed. Even if you could use
this approach you would still need a code line for each summation.

The loop I proposed could be run just once and each summation could be
completed in that, but you probably know that already, but are just trying
to avoid the coding!

I cannot think of another option at the moment.....

--

Regards,
Nigel
(E-Mail Removed)



"Damien McBain" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Thanks for that Nigel
>
> I was hoping not to have to do it like that because there are a couple of
> hundred cells I need to resolve in a similar manner. It's going to make
> the code very long and laborious (but maybe necessary).
>
> I thought I might be able to use the same principal as the worksheet
> function:
> =Sum(Sheet2:Sheet20!B8)
> ... but I don't know how to specify a multi sheet range like that in VBA
> (given that the sheet names in my workbook are dynamic - but always listed
> in the range "BusAreaList")
>
> cheers,
>
> Damien
>
>
> "Nigel" <nigel-(E-Mail Removed)> wrote in message
> news:728F418D-0410-40D4-BD05-(E-Mail Removed)...
>> No need to use collection, you say you already have a list of sheets in
>> the range BusAreaList so use that.
>>
>> Something like
>>
>> Dim c as Range, myTotal as double
>> myTotal = 0
>> For each c in Range("BusAreaList")
>> myTotal = myTotal + Sheets(c.value).Range("A1")
>> Next c
>>
>> The above adds the value in A1 on all sheets named in the range
>> BusAreaList to the variable myTotal. You might want to validate that the
>> list sheet names are valid.
>>
>>
>> --
>>
>> Regards,
>> Nigel
>> (E-Mail Removed)
>>
>>
>>
>> "Damien McBain" <(E-Mail Removed)> wrote in message
>> news:O67%(E-Mail Removed)...
>>> Hi,
>>> I need to add the same cells in multiple worksheets. I want the
>>> worksheets to be selected dynamically using a range on another worksheet
>>> which contains the names of the worksheets to sum. I don't want any
>>> formulas in the summary worksheet - I want to populate the cells with
>>> values.
>>>
>>> I'm thinking something like this to populate a Collection (I've never
>>> used collections before):
>>> --------------------
>>> Dim Allsheets As New Collection
>>> For Each branch In Worksheets("Tables").Range("BusAreaList")
>>> Allsheets.Add Item:=branch
>>> Next branch
>>> --------------------
>>> ...but I don't know how to use the Items in the Collection to specify
>>> which cells to sum (or even if I can!). Am I on the right track?
>>> Any help appreciated.
>>> TIA
>>> Damien
>>>

>>

>
>


 
Reply With Quote
 
Damien McBain
Guest
Posts: n/a
 
      24th Jan 2008
> The loop I proposed could be run just once and each summation could be
> completed in that, but you probably know that already, but are just trying
> to avoid the coding!


You got that right!

Thanks for taking the time to help.

cheers

Damien

"Nigel" <nigel-(E-Mail Removed)> wrote in message
news:0B43457B-37FE-40F0-BD56-(E-Mail Removed)...
> You can use the application worksheet function in VBA to replicate the
> =Sum(Sheet2:Sheet20!B8) to get the sum from multiple sheets, but as you
> say the sheets list is not contiguous nor is it fixed. Even if you could
> use this approach you would still need a code line for each summation.
>
> The loop I proposed could be run just once and each summation could be
> completed in that, but you probably know that already, but are just trying
> to avoid the coding!
>
> I cannot think of another option at the moment.....
>
> --
>
> Regards,
> Nigel
> (E-Mail Removed)
>
>
>
> "Damien McBain" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Thanks for that Nigel
>>
>> I was hoping not to have to do it like that because there are a couple of
>> hundred cells I need to resolve in a similar manner. It's going to make
>> the code very long and laborious (but maybe necessary).
>>
>> I thought I might be able to use the same principal as the worksheet
>> function:
>> =Sum(Sheet2:Sheet20!B8)
>> ... but I don't know how to specify a multi sheet range like that in VBA
>> (given that the sheet names in my workbook are dynamic - but always
>> listed in the range "BusAreaList")
>>
>> cheers,
>>
>> Damien
>>
>>
>> "Nigel" <nigel-(E-Mail Removed)> wrote in message
>> news:728F418D-0410-40D4-BD05-(E-Mail Removed)...
>>> No need to use collection, you say you already have a list of sheets in
>>> the range BusAreaList so use that.
>>>
>>> Something like
>>>
>>> Dim c as Range, myTotal as double
>>> myTotal = 0
>>> For each c in Range("BusAreaList")
>>> myTotal = myTotal + Sheets(c.value).Range("A1")
>>> Next c
>>>
>>> The above adds the value in A1 on all sheets named in the range
>>> BusAreaList to the variable myTotal. You might want to validate that
>>> the list sheet names are valid.
>>>
>>>
>>> --
>>>
>>> Regards,
>>> Nigel
>>> (E-Mail Removed)
>>>
>>>
>>>
>>> "Damien McBain" <(E-Mail Removed)> wrote in message
>>> news:O67%(E-Mail Removed)...
>>>> Hi,
>>>> I need to add the same cells in multiple worksheets. I want the
>>>> worksheets to be selected dynamically using a range on another
>>>> worksheet which contains the names of the worksheets to sum. I don't
>>>> want any formulas in the summary worksheet - I want to populate the
>>>> cells with values.
>>>>
>>>> I'm thinking something like this to populate a Collection (I've never
>>>> used collections before):
>>>> --------------------
>>>> Dim Allsheets As New Collection
>>>> For Each branch In Worksheets("Tables").Range("BusAreaList")
>>>> Allsheets.Add Item:=branch
>>>> Next branch
>>>> --------------------
>>>> ...but I don't know how to use the Items in the Collection to specify
>>>> which cells to sum (or even if I can!). Am I on the right track?
>>>> Any help appreciated.
>>>> TIA
>>>> Damien
>>>>
>>>

>>
>>

>



 
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
Sum cell value across multiple worksheets. Damian Microsoft Excel Misc 10 5th Feb 2010 08:46 PM
sum one cell from multiple worksheets =?Utf-8?B?YWJydXNleQ==?= Microsoft Excel Worksheet Functions 3 23rd Aug 2006 10:48 PM
how do I sum the same cell from multiple worksheets? =?Utf-8?B?cmJwZXJyaWU=?= Microsoft Excel Worksheet Functions 2 18th Feb 2005 05:53 PM
Same cell name in multiple worksheets drew Microsoft Excel Misc 8 14th May 2004 12:18 PM
Same cell name in multiple worksheets david Microsoft Excel Worksheet Functions 5 22nd Oct 2003 07:22 PM


Features
 

Advertising
 

Newsgroups
 


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