Is it possible to prevent user-made modifications of some 开发者_StackOverflowspecific cells? I can use VSTO add-in for plugging into Excel events.
Take a look at Worksheet.Change Event. This event takes a Range object. As far as I remember this objects contains information about what cell(s) is selected. Then you can do with this cell whatever you want link.
My approach is slightly different.
I protect the complete sheet and then unlock particular cells or columns or rows, so user can fill details in those sheets. But you should get same results with this.
Code has been tested on VS 2008.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
var activesheet = (Excel.Worksheet)this.Application.ActiveSheet;
// keep particular cell unlocked
activesheet.get_Range("A1",missing).Locked = false;
// keep entire row unlocked
activesheet.get_Range("B2", missing).EntireRow.Locked = false;
// keep entire column unlocked
activesheet.get_Range("C1", missing).EntireColumn.Locked = false;
// keep particular range (D4 - E8) unlocked
activesheet.get_Range("D4","E8").Locked = false;
activesheet.Protect(missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
When user clicks on the locked cell, excel throws alert message saying cell is locked. Its annoying for user. You may disable displayalerts
.
Just use
this.Application.DisplayAlerts = false;
in the ThisAddIn_Startup(object sender, System.EventArgs e)
just before declaring var activesheet
and do not forget the enable it in again using
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
this.Application.DisplayAlerts = true;
}
This will take care of the annoying alert message.
精彩评论