乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>EXCEL</em> <em>VBA</em>锁定工作表-exc

<em>EXCEL</em> <em>VBA</em>锁定工作表-exc

作者:乔山办公网日期:

返回目录:excel表格制作


你少了一句end if
ActiveSheet.Unprotect
Cells.Locked = False '先把所有单元格设置为不锁定的
For i = 3 To 63
If Cells(i, 1) = "正确" Then Cells(i, 2).Locked = True
Next
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

也可以改成以下代码
ActiveSheet.Unprotect
Cells.Locked = False '先把所有单元格设置为不锁定的
For i = 3 To 63
If Cells(i, 1) = "正确" Then Cells(i, 2).Locked = True
Next
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Me.Protect Password:="123", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True

注意:AllowFiltering:=True
'试试这样看行不行
Private Sub Worksheet_Change(ByVal Target As Range)
  If Range("a1").Locked = True Then
     If Range("a1") = "" Then
        Cells.Select
        Selection.Locked = False
     End If
   End If
    Me.Unprotect ("1") '撤销工作表保护,123为密码
    If Target <> "" Then
        Target.Locked = True
    End If
    Me.Protect ("1") '保护工作表
    ActiveSheet.Unprotect
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target <> "" Then
        ActiveSheet.Unprotect
    End If
End Sub


可以使用project方法,保护工作表。另外,要先使zhidao用locked属性和formulahidden来锁定和隐藏需要锁定或者隐藏的单元格区域
要填数据的可以不锁定,需要锁定的部分锁定并保护工作表了之后才可以的。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网