Avatar of zachvaldez
zachvaldez
Flag for United States of America asked on

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?
C#

Avatar of undefined
Last Comment
zachvaldez

8/22/2022 - Mon
Kyle Abrahams

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

Open in new window

zachvaldez

ASKER
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!!!
Rgonzo1971

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
zachvaldez

ASKER
Can Usedrange substitute on this or not? Does usedrange cover only to the last row of data or till the 65,000+
Last row?
Rgonzo1971

It depends on the data and usedrange is not very reliable
zachvaldez

ASKER
What if I want to change background color of "J1"? How to reference the cell?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rgonzo1971

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

Open in new window

zachvaldez

ASKER
Is column J 10?
Rgonzo1971

then try
Excel.Range xlRange= (Excel.Range) xlWorkSheet.Cells[1, 10];
xlRange.Interior.Color = Excel.XlRgbColor.rgbYellow;
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
zachvaldez

ASKER
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?
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
zachvaldez

ASKER
is  "as" a C# keyword? just asking
zachvaldez

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rgonzo1971

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

Open in new window

zachvaldez

ASKER
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...)
Rgonzo1971

replace with
xlsWorkSheet.UsedRange.Rows.Count
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
zachvaldez

ASKER
Bravo!