We help IT Professionals succeed at work.

Unlocking a column in excel using C#

zachvaldez
zachvaldez asked
on
183 Views
Last Modified: 2017-03-23
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

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

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!!!
CERTIFIED EXPERT
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

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?
CERTIFIED EXPERT
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?
CERTIFIED EXPERT
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?
CERTIFIED EXPERT
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?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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

Commented:
replace with
xlsWorkSheet.UsedRange.Rows.Count

Author

Commented:
Bravo!