Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Varying a Formula in an Excel Spreadsheet

I have been trying to insert formula into the cells of an Excel spreadsheet
that sums the contents of cells from a number of other Excel spreadsheets,
within the same workbook. I would like to extend this to apply to other
speadsheets were the number of spreadsheets within the formula varies.
i.e. Spreadsheet 'sheet1' might source its information from 2 spreadsheets,
whilst spreadsheet 'sheet2' might want to source its infromation from 3
spreadsheets. I also set up a numebr of for_next loops to control the row
and column of the cells which require to be populated and information
sourced from.

I tried to define a string variable 'eqn' which I hoped could be placed in
the formula in one line of code such that this could be applied to either
of the spreadsheets. The string variable could then be altered to reflect
the number of source spreadsheets required.

I tried the following:

For Each sheet In Array ("sheet1","sheet2")
For Each column In Array ("A","B","C")
For Each row In Array ("1","2","3")

If sheet = "sheet1" then eqn = "'sheet10'!" & column & row &
",'sheet11'!" & column & row &"

If sheet = "sheet2" then eqn = "'sheet10'!" & column & row &
",'sheet11'!" & column & row & "'sheet12'!" & column & row"


sheet.Cells("row","column").Formula = "=SUM(" & eqn &")"

Next
Next
Next

The intention was to insert a formula in the relevant cells of 'sheet1', in
the example, which looked something like this:

"=SUM('sheet10'!" & column & row & ",'sheet11'!" & column & row & ")"

Excel dosen't seem to recognise the use of string variables used in this
way. Could anyone suggest an alternatiive or suggest where I might be
going wrong.

--
Message posted via http://www.officekb.com
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Varying a Formula in an Excel Spreadsheet

You just had some typos and syntax errors. this worked fine for me:

Sub ABC()
For Each sheet In Array("sheet1", "sheet2")
For Each Column In Array("A", "B", "C")
For Each Row In Array("1", "2", "3")

If sheet = "sheet1" Then eqn = _
"'sheet10'!" & Column & Row & ",'sheet11'!" & _
Column & Row & ""

If sheet = "sheet2" Then eqn = _
"'sheet10'!" & Column & Row & ",'sheet11'!" & _
Column & Row & ",'sheet12'!" & Column & Row & ""


Worksheets(sheet).Cells(Row, Column) _
.Formula = "=SUM(" & eqn & ")"

Next
Next
Next

End Sub

--
Regards,
Tom Ogilvy

"Ardvaark the Third via OfficeKB.com" wrote in message
...
I have been trying to insert formula into the cells of an Excel

spreadsheet
that sums the contents of cells from a number of other Excel spreadsheets,
within the same workbook. I would like to extend this to apply to other
speadsheets were the number of spreadsheets within the formula varies.
i.e. Spreadsheet 'sheet1' might source its information from 2

spreadsheets,
whilst spreadsheet 'sheet2' might want to source its infromation from 3
spreadsheets. I also set up a numebr of for_next loops to control the row
and column of the cells which require to be populated and information
sourced from.

I tried to define a string variable 'eqn' which I hoped could be placed in
the formula in one line of code such that this could be applied to either
of the spreadsheets. The string variable could then be altered to reflect
the number of source spreadsheets required.

I tried the following:

For Each sheet In Array ("sheet1","sheet2")
For Each column In Array ("A","B","C")
For Each row In Array ("1","2","3")

If sheet = "sheet1" then eqn = "'sheet10'!" & column & row &
",'sheet11'!" & column & row &"

If sheet = "sheet2" then eqn = "'sheet10'!" & column & row &
",'sheet11'!" & column & row & "'sheet12'!" & column & row"


sheet.Cells("row","column").Formula = "=SUM(" & eqn &")"

Next
Next
Next

The intention was to insert a formula in the relevant cells of 'sheet1',

in
the example, which looked something like this:

"=SUM('sheet10'!" & column & row & ",'sheet11'!" & column & row & ")"

Excel dosen't seem to recognise the use of string variables used in this
way. Could anyone suggest an alternatiive or suggest where I might be
going wrong.

--
Message posted via http://www.officekb.com



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
Formula for selecting a varying cell reference Leona Excel Discussion (Misc queries) 2 February 20th 10 12:51 AM
can th same spreadsheet have varying cell sizes top & bottom 530i Excel Discussion (Misc queries) 2 July 29th 09 10:41 PM
Pivotting - Formula w/ Varying Column Bigfoot17 Excel Worksheet Functions 7 October 3rd 08 06:39 PM
copy data from varying slected rows into new spreadsheet standard_guy Excel Discussion (Misc queries) 2 April 6th 08 04:58 PM
Excel formula for varying payment interest bear installment loan. mtgburns Excel Discussion (Misc queries) 0 November 21st 07 03:40 PM


All times are GMT +1. The time now is 10:30 PM.

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"