VBA to create formula from a list in one sheet.

S

Scott French

I am looking for a script that will create a formula that adds up cells from
multiple sheets and places the formula on a totals sheet. The script needs to
grab the name of the sheets from a range on a seperate sheet in the workbook.

I hope this make sense.
 
B

Bob Phillips

Range("A1").Formula =
"=SUMPRODUCT(SUMIF(INDIRECT(""'""&C1:C3&""'!C8""),""<>""))"

where C1:C3 is a range housing the relevant sheetnames in separate cells,
and C* is the cell to add.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
S

Scott French

Thanks Bob that worked.
One tweek I can't figure out is how to get the script to create the range
"C1:C3" in the formula automatically. The list can vary from 1 to many sheet
names. I tried to C1:C50 but if cells below C5 are empty you get a REF error.

TIA

Scott
 
B

Bob Phillips

Scott,

This should do it

Dim LastRow As Long
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Range("A1").Formula = _
"=SUMPRODUCT(SUMIF(INDIRECT(""'""&C1:C" & LastRow & "&""'!C8""),""<>""))"



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top