Solved

Create excel formula in C# code

Posted on 2011-03-22
10
783 Views
Last Modified: 2012-06-27
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(worksheet.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

0
Comment
Question by:Halon
  • 6
  • 4
10 Comments
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35192367
use the property FormulaR1C1 instead of Formula ...
0
 

Author Comment

by:Halon
ID: 35193346
That works great to get the value into the cell

 ws.get_Range(ws.Cells[curRowIx, 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.

0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35193375
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... :)
0
 

Author Comment

by:Halon
ID: 35193405
='SN21'!C3

I need to increment all 3

eb - ='SN21'!C4 or ='SN22'!C4, or ='SN21'!D4 etc etc

Thanks!!
0
 

Author Comment

by:Halon
ID: 35193417
More info, sorry. I need to be able to change worksheet name, Column and row in loops.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 500 total points
ID: 35193464
the equivalent formula would be:
...FormulaR1C1 = "='SN21'!R3C3"

you can work with R3C3 (row 3 column 3) instead of C3 (the cell address...)
when you use absolute numbers like that, you're meaning the exact cell address.

when you use like:
...FormulaR1C1 = "='SN21'!R[3]C[-1]"
(with these square brackets) it means relative reference to the cell you're putting the formula in...
[3] is a positive number, would mean 3 rows down
[-1] is a negative number, would mean 1 column to the left


see, since the whole formula would be a string, you can treat it as you want...
Even change the 21 to 22 or whatever...
0
 

Author Comment

by:Halon
ID: 35193503
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.
0
 

Author Closing Comment

by:Halon
ID: 35193506
Great Resource!!
0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35193539
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... :-)
0
 

Author Comment

by:Halon
ID: 35193548
I, of course, meant you were the resource sir! haha
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete texts with font color 16 30
Highlighting cells in Excel 9 16
Excel case statements 3 24
splitting text of cell to columns 14 24
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now