need help writing to a protected cell in Excel

mjd430

Member
Joined
Feb 28, 2007
Messages
6
Programming Experience
Beginner
Hello,

I have an application that is written in VB.net 2005 and interacts with MS Excel 2002 on a Windows XP SP3 machine. My app opens an excel spreadsheet, sends data to specific cells, saves and closes the spreadsheet. My business customer asked that one of the cells (named zipcode) be "disabled" so that the user cannot change the data in that cell. I figured I could protect the cell since the sheet is also password protected and still be able to send the zipcode to the cell. This is the error that I get when I try to do that:

"The cell or chart you are trying to change is protected and therefore read-only.
To modify a protected cell or chart, first remove protection using the Unprotect Sheet command (Tools menu, Protection submenu). You may be prompted for a password."

This makes sense to me, but I was looking to find a way to unprotect the cell, send the data, and then protect it again. Is there any way to do that? I haven't been able to find much help on the internet.

Thanks!
 
Thanks JohnH. I found the protect/unprotect methods before, but I couldn't figure it out. I was making it much more difficult than it needed to be.

Here's what I did:

Dim ws As Excel.Worksheet = ExcelWorkbook.Worksheets("Matrix")
ws.Unprotect("pw")
'write to excel here
ws.Protect("pw")

I thought I needed to unprotect the exact cell I was writing to. I found that wasn't the case. Thanks again!
 
That is the preparation you do before protecting the sheet. In automation programming that relates to the Locked property of a Range object.
 
Back
Top