0

我有一张未受保护的工作表,以便用户可以在每个字段中输入信息,但我需要保护工作表不被排序。排序似乎搞砸了工作表中的公式和链接。我已经看到了很多关于如何保护工作表但允许排序的帮助,但不是我的问题,这与此相反。

看起来应该很简单;这就是我现在所拥有的:

Sub ProtectRevHistory()

Worksheets("Revision History").Protect Contents:=False, AllowSorting:=False, UserInterfaceOnly:=True

End Sub

我没有得到任何错误,但它不保护排序。这甚至可能吗?

最终,如果我可以让它工作,我想从Workbook_Open()事件中运行它,但是当我将此代码放在 ThisWorkbook 对象中时,它会在打开时要求我输入密码,即使我关闭了 Password 参数。只需按 enter 输入密码就会给我一个错误,并且也不能保护排序。

4

1 回答 1

0

So I broke down and used the macro editor to see if that would help me figure this out and I did get it working. After that, I was able to put it in the Workbook Open event so that it runs on open. Here's what it looks like:

Private Sub Workbook_Open()


Sheets("Revision History").Protect Password:="StrongPasswordHere", DrawingObjects:=False, Contents:=True, Scenarios:= _
    False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
    :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
    AllowDeletingRows:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
    True


End Sub

Maybe there is something about the parameters that I don't fully understand that makes it work when all of them are listed. I'm not sure. But at least this is out there now for anyone else who needs it! The nice thing about it is that all of the Sorting functions on the Data tab are greyed out. And if you try to sort in the Filter, it doesn't work either. Success!!

于 2013-05-21T19:08:45.857 回答