![头像](/Images/mod/002.gif)
-
积分:0
-
财富值:2.0
-
身份:普通用户
我想实现的功能是:
选中某一单元格或区域,点右键“锁定”,然后选中部分就被锁定了(可以标示背景色以区分被锁定状态);
再次选中已被锁定的区域,点右键“解锁”,选中部分就被去锁定了。
我有一个加载宏,但是使用起来,还是不够方便。希望大侠们加以修改,以更加便于使用。
代码如下:
Sub lockadd()
For Each myctl In Application.CommandBars("cell").Controls
If myctl.ID = 888 Then Exit Sub
Next
With Application.CommandBars("cell").Controls.Add(ID:=888, before:=1)
.Caption = "锁定单元格"
.BeginGroup = True
.OnAction = "lockRange"
End With
End Sub
Sub Allreset()
Application.CommandBars("cell").reset
End Sub
Sub unlockadd()
For Each myctl In Application.CommandBars("cell").Controls
If myctl.ID = 888 Then Exit Sub
Next
With Application.CommandBars("cell").Controls.Add(ID:=888, before:=1)
.Caption = "解锁单元格"
.BeginGroup = True
.OnAction = "lockRange"
End With
End Sub
Sub lockRange()
On Error Resume Next
ActiveSheet.Unprotect
Dim myrange As Range
Set myrange = Selection
If myrange.Locked Then
myrange.Locked = False
myrange.Interior.ColorIndex = xlNone
Else
myrange.Locked = True
myrange.Interior.ColorIndex = 20
End If
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub