简体   繁体   English

在工作簿中保护启用了宏的工作表

[英]Protecting Macro Enabled worksheet in a workbook

I have a 4 sheet workbook of employee over time. 随着时间的推移,我有一份4张员工的工作簿。 The first 3 sheets are where the data is added by others. 前三张纸是其他人添加数据的地方。 The 4th sheet is where the data is added up. 第四张纸是数据相加的地方。 On this 4th sheet I have enabled an auto sort macro to constantly reorder the data I have on several different tables. 在第4张纸上,我启用了自动排序宏以不断对我在几个不同表上的数据进行重新排序。 Id like to protect this 4th sheet to make it tamper proof, but I found that whenever I protect the sheet, the formulas still work, however the macro does not. 我想保护第4个工作表以防篡改,但是我发现只要保护该工作表,公式仍然有效,但是宏不起作用。 I know the macro works because whenever I unprotect the sheet the macro activates again. 我知道宏有效,因为每当我取消保护工作表时,宏都会再次激活。 Do I need to add something to my macro to make it work in protected mode or am I simply doing something wrong or this something that is not capable with excel 2010? 我是否需要在宏中添加一些内容以使其在受保护的模式下工作,还是我只是做错了某事,或者这是Excel 2010无法提供的? This is the macro im currently using: 这是即时消息当前正在使用的宏:

Private Sub Worksheet_Activate()
On Error Resume Next
    Range("c1").Sort Key1:=Range("c2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
On Error Resume Next
    Range("k1").Sort Key1:=Range("k2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

On Error Resume Next
    Range("o1").Sort Key1:=Range("o2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
On Error Resume Next
    Range("s1").Sort Key1:=Range("s2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End Sub

You've got a friend you can work with. 您有一个可以与之合作的朋友。 This friend is called userinterfaceonly . 这个朋友称为userinterfaceonly

At the beginning of your code, add the following: 在代码的开头,添加以下内容:

    ActiveSheet.Unprotect Password:="Whatever"
    ActiveSheet.Protect _
        Password:= "Whatever", _
        userinterfaceonly:=True

This will remove sheet protection, then reactivate it with the UserInterfaceOnly property, which allows changes to be made by the macro, but not by the user. 这将删除工作表保护,然后使用UserInterfaceOnly属性将其重新激活,该属性允许由宏而不是用户进行更改。 Unfortunately, this property can't be saved, so we need the code to either run whenever the file is opened, or when the worksheet is activated. 不幸的是,该属性无法保存,因此我们需要代码在打开文件或激活工作表时运行。

Yea you need to unlock the sheet before you call the sort function I think 是的,您需要先解锁工作表,然后再调用我认为的排序功能

Private Sub Worksheet_Activate()
    Set sheet = ActiveSheet
    sheet.Unprotect Password:="password"
    sheet.Range({whatever range you want}).Locked = False    
    On Error Resume Next
        Range("c1").Sort Key1:=Range("c2"), _
        Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    On Error Resume Next
      Range("k1").Sort Key1:=Range("k2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
sheet.protect Password:="password"
sheet.Range({whatever range you want}).Locked = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM