我得到了我在谷歌找到的这段代码。(http://msgroups.net/microsoft.public.excel.misc/locking-cells-automatically-after/45998)我的工作需要这个。-编辑:我对 Excel 的了解为零
网格中的所有单元格目前都未锁定。我想要做的是在输入数据后自动锁定单元格。
我需要让代码最多可用于列“J”并且对于行是无限的。我在谷歌得到的这段代码最多只能是 25x25:如果单元格是空白或空白,密码保护应该不起作用,即使之前有数据也可以自由输入。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim myCell As Range
Dim myIntersect As Range
Dim myPassword As String
myPassword = "hi there"
With Me 'the worksheet with the code
'25 rows by 25 columns starting in A1
Set myRng = .Range("a1").Resize(25, 25)
Set myIntersect = Intersect(Target, myRng)
If myIntersect Is Nothing Then
Exit Sub
End If
.Unprotect Password:=myPassword
For Each myCell In myIntersect.Cells
myCell.Locked = True
Next myCell
.Protect Password:=myPassword
End With
End Sub