Winston Smith
asked on
Create excel formula in C# code
I have an app that creates excel workbooks, filling with data, graphs etc no problem.
I now want to create a formula on Sheet2 that references a cell on Sheet1.
I can do it no problem with the following line of code
worksheet.get_Range(worksh eet.Cells[ curRowIx, curColIx], worksheet.Cells[curRowIx, curColIx]).Formula = "='SN21'!C" + startRow;
using the startRow in the formula i can loop through the rows no problem and it works. What i need is to be able to access the column in a loop also. Not using C1 but R[x]C[y] so i can loop through all data i need and populate the formulas correctly.
I know i can hardcode all the A1, B1 ... etc but that makes me nauseous and i hate repetitive code. I want to use something like
.Formula = "='SN21'" + worksheet.Cells[curRowIx, curColIx] but i cant get it to work
Thanks in advance
I now want to create a formula on Sheet2 that references a cell on Sheet1.
I can do it no problem with the following line of code
worksheet.get_Range(worksh
using the startRow in the formula i can loop through the rows no problem and it works. What i need is to be able to access the column in a loop also. Not using C1 but R[x]C[y] so i can loop through all data i need and populate the formulas correctly.
I know i can hardcode all the A1, B1 ... etc but that makes me nauseous and i hate repetitive code. I want to use something like
.Formula = "='SN21'" + worksheet.Cells[curRowIx, curColIx] but i cant get it to work
Thanks in advance
use the property FormulaR1C1 instead of Formula ...
ASKER
That works great to get the value into the cell
ws.get_Range(ws.Cells[curR owIx, curColIx], ws.Cells[curRowIx, curColIx]).FormulaR1C1 = ws2.Cells[curRowIx-1, curColIx + 1];
But it does not place a formula on the cell, which is what i need for an audit trail of where data comes from for whoever is looking at the sheet.
ws.get_Range(ws.Cells[curR
But it does not place a formula on the cell, which is what i need for an audit trail of where data comes from for whoever is looking at the sheet.
you just have to write the formula in a way that excel will understrand...
what is your formula inside excel supposed to be ?
just put it here that I will translate it to you... :)
what is your formula inside excel supposed to be ?
just put it here that I will translate it to you... :)
ASKER
='SN21'!C3
I need to increment all 3
eb - ='SN21'!C4 or ='SN22'!C4, or ='SN21'!D4 etc etc
Thanks!!
I need to increment all 3
eb - ='SN21'!C4 or ='SN22'!C4, or ='SN21'!D4 etc etc
Thanks!!
ASKER
More info, sorry. I need to be able to change worksheet name, Column and row in loops.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
VERY cool! I did not know this.
One thing to ask first, when you talk about relative to the cell i am putting the formula in. If i am putting a formula in cell 1,1 on sheet2 but pulling data from cell 1,1 on sheet1 then the formula would be
FormulaR1C1 = "='Sheet2'!R[0]C[0]"?
And if it was from one row down and one column left it would be
FormulaR1C1 = "='Sheet2'!R[1]C[1]"?
Thanks for this, very quick and precise help.
One thing to ask first, when you talk about relative to the cell i am putting the formula in. If i am putting a formula in cell 1,1 on sheet2 but pulling data from cell 1,1 on sheet1 then the formula would be
FormulaR1C1 = "='Sheet2'!R[0]C[0]"?
And if it was from one row down and one column left it would be
FormulaR1C1 = "='Sheet2'!R[1]C[1]"?
Thanks for this, very quick and precise help.
ASKER
Great Resource!!
positive in the bracket means row down or column to the right
negative in the bracket means row above or column to the left
you you use numbers without brackets, it's absolute reference...
like:
if you put in cell Range("A1").FormulaR1C1= "=R5C10"
the actual formula in the cell will be =$J$5
if you put in cell Range("A1").FormulaR1C1= "=R[5]C[10]"
the actual formula in the cell will be =K6
Yeah, this is a great resource by Excel...
You're welcome... :-)
negative in the bracket means row above or column to the left
you you use numbers without brackets, it's absolute reference...
like:
if you put in cell Range("A1").FormulaR1C1= "=R5C10"
the actual formula in the cell will be =$J$5
if you put in cell Range("A1").FormulaR1C1= "=R[5]C[10]"
the actual formula in the cell will be =K6
Yeah, this is a great resource by Excel...
You're welcome... :-)
ASKER
I, of course, meant you were the resource sir! haha