Unlocking a column in excel using C#

I have the Microsoft .Office.Interop.Excel installed but my question is
I want to unlock a column H. I do have data to reach H1..H500  but it could be less rows.
I want to unlock only until the last row with data. It should not affect more than the last row with data.
whats the code?
zachvaldezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
try the following:
Excel.Range all = app.get_Range("H:H", Type.Missing);
all.Locked = false;

Open in new window

0
zachvaldezAuthor Commented:
I want to put gridlines in the column. Right now column is solid white with no gridlines. like the column look normal .
what is the code to do that in addition to above. THANKS!!!
0
Rgonzo1971Commented:
Hi,

pls try
        xlRange = (Excel.Range)xlWorkSheet.get_Range(xlWorkSheet.Cells[1, 8] as Excel.Range, (xlWorkSheet.Cells[xlWorkSheet.Cells.Rows.Count, 8] as Excel.Range).get_End(Excel.XlDirection.xlUp));

        xlRange.Locked = false;
        xlRange.Cells.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuou

Open in new window

Kyle's code changes the whole column

 Regards
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

zachvaldezAuthor Commented:
Can Usedrange substitute on this or not? Does usedrange cover only to the last row of data or till the 65,000+
Last row?
0
Rgonzo1971Commented:
It depends on the data and usedrange is not very reliable
0
zachvaldezAuthor Commented:
What if I want to change background color of "J1"? How to reference the cell?
0
Rgonzo1971Commented:
then try
Excel.Range xlRange= (Excel.Range) xlWorkSheet.Cells[1, 8];
xlRange.Interior.Color = Excel.XlRgbColor.rgbYellow;

Open in new window

0
zachvaldezAuthor Commented:
Is column J 10?
0
Rgonzo1971Commented:
then try
Excel.Range xlRange= (Excel.Range) xlWorkSheet.Cells[1, 10];
xlRange.Interior.Color = Excel.XlRgbColor.rgbYellow;
0
zachvaldezAuthor Commented:
It's not changing the color. Originally it was yellow on J1. Can the formatting start on J2 ..down instead because J1 is a header with background yellow?
0
Rgonzo1971Commented:
then try
        xlRange = (Excel.Range)xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 10] as Excel.Range, (xlWorkSheet.Cells[xlWorkSheet.Cells.Rows.Count, 10] as Excel.Range).get_End(Excel.XlDirection.xlUp));

        xlRange.Locked = false;
        xlRange.Interior.Color =  Microsoft.Office.Interop.Excel.XlRgbColor.rgbYellow; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zachvaldezAuthor Commented:
is  "as" a C# keyword? just asking
0
zachvaldezAuthor Commented:
this code:
range = xlsWorkSheet.UsedRange.Columns[10, Type.Missing]; covers whole column including header. Can you modify this to start at J2 instead because J1 is header and has the original yellow background.
Maybe this is easier to understand for me.
0
Rgonzo1971Commented:
range = xlsWorkSheet.UsedRange.Columns[10, Type.Missing].Resize(xlsWorkSheet.UsedRange.Rows -1 , 1).Offset(1); 

Open in new window

0
zachvaldezAuthor Commented:
Excel.Range range4 = xlsWorkSheet.UsedRange.Columns[10, Type.Missing].Resize(xlsWorkSheet.UsedRange.Rows - 1, 1).Offset(1);

>>after the Resize, I have red underline on (xlsWorkSheet.UsedRange.Rows

getting this message ..(operator '-' cannot be applied to operands...)
0
Rgonzo1971Commented:
replace with
xlsWorkSheet.UsedRange.Rows.Count
0
zachvaldezAuthor Commented:
Bravo!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.