高手入:EXCEL中锁定的问题,用宏解决

制作宏用来锁定A4到C10,A13到C19这是二个不同的单元格区域在C3,和C12制作二个按扭,用来指定宏,我的构思是这样的在A4到C19区域(或A13到C19)输入内容后,按下C3(或C12)就不允许更改,要更改就提示请输入密码,请高手赐教

下面的代码,分别为两个按钮的宏。

密码暂定为“123”。你可以自己改。

Dim IS_FIRST As Long

Option Explicit

Sub Macro1()

'

' Macro1 Macro

' Macro记録日 : 2012/6/6

' Button1 Event

'

    Dim i As Long

    

    If ActiveSheet.ProtectContents Then

        ActiveSheet.Unprotect

    End If

    

    If IS_FIRST = 0 Then

        Cells.Locked = False

        Selection.FormulaHidden = False

        

        For i = 1 To ActiveSheet.Protection.AllowEditRanges.Count

            ActiveSheet.Protection.AllowEditRanges(i).Delete

        Next i

    End If

    

    Range("A4:C10").Locked = True

    Selection.FormulaHidden = False

    

    ActiveSheet.Protection.AllowEditRanges.Add Title:="范囲" & IS_FIRST, Range:=Range( _

        "A4:C10"), Password:="123"

        

    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

    

    IS_FIRST = IS_FIRST + 1

    

End Sub

Sub Macro2()

'

' Macro2 Macro

' Macro记録日 : 2012/6/6

' Button2 Event

'

    Dim i As Long

    If ActiveSheet.ProtectContents Then

        ActiveSheet.Unprotect

    End If

    

    If IS_FIRST = 0 Then

        Cells.Locked = False

        Selection.FormulaHidden = False

        

        For i = 1 To ActiveSheet.Protection.AllowEditRanges.Count

            ActiveSheet.Protection.AllowEditRanges(i).Delete

        Next i

    End If

    

    Range("A13:C19").Locked = True

    Selection.FormulaHidden = False

    

    ActiveSheet.Protection.AllowEditRanges.Add Title:="范囲" & IS_FIRST, Range:=Range( _

        "A13:C19"), Password:="123"

        

    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

    

    IS_FIRST = IS_FIRST + 1

    

End Sub 

还有一张效果图的,不过好像只能传一张图片。

温馨提示:答案为网友推荐,仅供参考
相似回答