受保护和共享工作表上的宏

时间:2012-11-26 14:46:34

标签: vba excel-vba excel-2002 excel

我在excel 2012中有一个共享和受保护的电子表格。(受保护但没有密码) 我写了这个宏:

Private Sub CommandButton1_Click()
    ActiveSheet.Unprotect
    Range("B4:K38").Select
    Selection.Sort Key1:=Range("D4"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
   'cells I don't want to be protect (to be user-editable)
    Range("B4:K38").Locked = False
   'protecting the sheet, and all the other cells will be protected
    Protect UserInterfaceOnly:=True
End Sub

但每次我使用这个宏时,我都会收到一条消息,告诉我方法保护有错误。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个,自移动以来没有经过测试

而不是使用protect userinterfaceonly:=true

    with activesheet
         .EnableSelection = xlUnlockedCells 
         .Protect UserInterfaceOnly:=True 
    End With

同样使用UserInterfaceOnly,您不需要取消保护工作表以使用vba对其进行编辑。