作者:乔山办公网日期:
返回目录:excel表格制作
打开该文件,先解除默认的“宏禁用”状态,再次点击“宏”→“录制宏”。
Sub Macro1()
'当D1列显百示为“正确度”时,用VBA锁定A1、B1、C1列的问数据答,专
'下面空白处属a2\b2\c2\d2可以自由录入数据
Dim ir As Integer
Dim c As Range
ActiveSheet.Unprotect
ir = [a65536].End(xlUp).Row
Cells.Locked = False
For Each c In Range("d1:d" & ir)
If c.Value = "正确" Then Range("A" & c.Row & ":C" & c.Row).Locked = True
Next
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
sheets("Sheet1").Unprotect("密码") '对表格 Sheet1 输入密码取消保护
sheets("Sheet1").Protect ("密码") '对表格 Sheet1输入密码 加密
保护工作表后,复锁定才起作用制。所以应解除第一百列的锁定,再保护工作表。
然后度Alt+F11,将下面代码粘贴到某个工问作表处。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
ActiveSheet.Unprotect
Range("A:A").Select
If Cells(Target.Row, Target.Column) = "" Then
Selection.Locked = False
Else
Selection.Locked = True
Cells(Target.Row, Target.Column).Locked = False
End If
Cells(Target.Row, Target.Column).Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End Sub
程序修改答的是A列