Unlocking a column in excel using C#

zachvaldez
zachvaldez used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer

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

Open in new window

Author

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!!!
Top Expert 2016

Commented:
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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

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?
Top Expert 2016

Commented:
It depends on the data and usedrange is not very reliable

Author

Commented:
What if I want to change background color of "J1"? How to reference the cell?
Top Expert 2016

Commented:
then try
Excel.Range xlRange= (Excel.Range) xlWorkSheet.Cells[1, 8];
xlRange.Interior.Color = Excel.XlRgbColor.rgbYellow;

Open in new window

Author

Commented:
Is column J 10?
Top Expert 2016

Commented:
then try
Excel.Range xlRange= (Excel.Range) xlWorkSheet.Cells[1, 10];
xlRange.Interior.Color = Excel.XlRgbColor.rgbYellow;

Author

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?
Top Expert 2016
Commented:
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

Author

Commented:
is  "as" a C# keyword? just asking

Author

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.
Top Expert 2016

Commented:
range = xlsWorkSheet.UsedRange.Columns[10, Type.Missing].Resize(xlsWorkSheet.UsedRange.Rows -1 , 1).Offset(1); 

Open in new window

Author

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...)
Top Expert 2016

Commented:
replace with
xlsWorkSheet.UsedRange.Rows.Count

Author

Commented:
Bravo!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial