Solved

Set value in a excel worksheet to equal the length of the previous column in C#

Posted on 2016-10-03
10
58 Views
Last Modified: 2016-11-01
I have a excel workbook with multiple worksheets.I would like to set one column H to be a formula field which is equal to the length of column G. How can I do it using C#.

Thanks,
Aditya
0
Comment
Question by:giveindia
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41826149
Hi,

pls try something like this
string s = "=LEN(RC[-1])";
Range rng1 = sheet.get_Range(sheet.Cells[7, 2], (sheet.Cells[7, 2] as Excel.Range).get_End(Excel.XlDirection.xlDown)).Offset[0,1];
rng1.FormulaR1C1 = s;
rng1.Value2 = rng1.Value2;

Open in new window

Regards
0
 

Author Comment

by:giveindia
ID: 41826219
What is value1 and value2 here ?
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41826222
.Value2 gives you the underlying value of the cell (could be empty, string, error, number (double) or boolean)

.Value gives you the same as .Value2 except if the cell was formatted as currency or date it gives you a VBA currency (which may truncate decimal places) or VBA date.

Using .Value is usually a bad idea because you may not get the real value from the cell, and they are slower than .Value2
0
 

Author Comment

by:giveindia
ID: 41826238
So the first time the excel is saved the value gets saved correctly. If I open the excel and edit a cell the length column does not get updated.

Thanks,
Aditya
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41826247
pls try
        string s = "=LEN(RC[-1])";
        Rng1= (Excel.Range)xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 7] as Excel.Range, (xlWorkSheet.Cells[2, 7] as Excel.Range).get_End(Excel.XlDirection.xlDown)).Offset[0,1];
        Rng1.FormulaR1C1 = s;
        Rng1.Value2 = Rng1.Value2;

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:giveindia
ID: 41826250
THis is the code that I have.
 string s = "=LEN(RC[-1])";
                               Microsoft.Office.Interop.Excel.Range rng1 = wsheet.get_Range(wsheet.Cells[2, 7], (wsheet.Cells[dsMDOID.Tables[0].Rows.Count + 1, 7] as Excel.Range).get_End(Excel.XlDirection.xlDown)).Offset[0, 1];
                               rng1.FormulaR1C1 = s;
                               rng1.Value2 = rng1.Value2;
0
 

Author Comment

by:giveindia
ID: 41826257
Also even if  I have 20 rows in the sheet it seems to apply the formula to around 65K rows.
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41826355
then try
  string s = "=LEN(RC[-1])";
Microsoft.Office.Interop.Excel.Range rng1 = wsheet.get_Range(wsheet.Cells[2, 7], (wsheet.Cells[dsMDOID.Tables[0].Rows.Count + 1, 7] as Excel.Range)).Offset[0, 1];
rng1.FormulaR1C1 = s;
rng1.Value2 = rng1.Value2; 

Open in new window

0
 

Author Comment

by:giveindia
ID: 41827435
The code you posted seems to be similar to what I had.

Thanks,
Adittya
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41827470
get_end removed
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

758 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

21 Experts available now in Live!

Get 1:1 Help Now