Saturday, April 3, 2010

VBScript - Excel Individual Cell Protection from Changes without protecting the worksheet

Excel Individual Cell Protection from Changes without protecting the worksheet.

'*******************************************************************************************

' Proc/Function Name : Workbook_SheetChange

' Scope : Private

' Description : This event is triggered when a change is happened.

' Reference : clsProtector

' Author : SS. Kanagal

' I/P Parameters : ByVal Sh As Object, ByVal Target As Range

' O/P Parameters : -NIL-

' Version History : Date Version Remarks

' : Dec. 10, 2009 1.0 Initial release

'*******************************************************************************************

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'If the changes are done to the columns to protect list in cell B1 then allow changes.
If StrComp(LCase(Replace(Target.Address, "$", "")), "b1") = 0 Then Exit Sub
'Activate the current workbook.
ThisWorkbook.Activate
With ThisWorkbook.Worksheets(1)
'Chk the target cell is allowed to accept the changes?
If InStr(LCase(.Cells(1, 2)), LCase(Replace(Target.Address, "$", ""))) > 0 Then
'Target cell is not allowed to accept the changes then throw off the new changes.
With Application
.EnableEvents = False 'Disable the events
.Undo 'Revert back the changes
.EnableEvents = True 'Enable the events
End With 'Application
End If 'InStr(LCase(.Cells(1, 2)), LCase(CellAddrRequestingChange)) > 0 Then
End With 'WrkBook.Worksheets(WrkSheet)
End Sub

[Via]