简体   繁体   English

防止用户使用 Excel 内置排序功能

[英]Prevent user from using Excel Built-in Sort functions

I have a document that functions (using many already written and functioning Macros) to essentially stand as a visual representation of a calendar, which can then be exported to a powerpoint graphic.我有一个文档,它的功能(使用许多已经编写和运行的宏)本质上是作为日历的可视化表示,然后可以将其导出为 powerpoint 图形。 Because of the way this process is conducted, it is essential that empty rows are kept between each data row.由于此过程的执行方式,必须在每个数据行之间保留空行。 I wrote my own sort and filter functions to preserve this condition while giving the user proper functionality, and now am working on document security.我编写了自己的排序和过滤函数来保留这种情况,同时为用户提供适当的功能,现在正在研究文档安全性。

Thus, the primary question I have (although this can be spread more generally to all worksheet actions) is whether or not it is possible to restrict users from ever using the default Sort functions from excel (as this would immediately destroy the data structure)?因此,我的主要问题(尽管这可以更广泛地扩展到所有工作表操作)是是否可以限制用户使用 Excel 中的默认排序函数(因为这会立即破坏数据结构)?

To clarify, the sheet already has protect and unprotect functionality, but I need the AllowSorting property to remain false even when the sheet is unprotected .澄清一下,工作表已经具有保护和取消保护功能,但即使工作表不受保护,我也需要AllowSorting属性保持 false 。 Is this possible?这可能吗?

Edit:编辑:
I attempted to solve the issue by not unprotecting the sheets, but instead protecting them with all protections "off" with the exception of the AllowSorting and AllowFiltering properties, which remain false in both the Locked and Unlocked configurations.我试图通过不取消保护工作表来解决这个问题,而是通过“关闭”所有保护来保护它们,除了 AllowSorting 和 AllowFiltering 属性,它们在锁定和解锁配置中都保持为假。 However, this still allowed me to sort data when the sheets were "unlocked"但是,这仍然允许我在工作表“解锁”时对数据进行排序

Edit 2: Since further explanation seems necessary, I already have Macros that protect and unprotect the sheet accordingly.编辑 2:由于似乎有必要进一步解释,我已经有了相应地保护和取消保护工作表的宏。 I need to be able to give FULL functionality to any user who has the correct password, with the exception of sorting , as no user should ever use the sort functions.我需要能够为任何拥有正确密码的用户提供完整的功能,排序除外,因为任何用户都不应该使用排序功能。 My code for these functions is shown (abbreviated) below:我的这些函数的代码如下所示(缩写):

Sub LockSheets()

For Each WS In ActiveWorkbook.Worksheets
        WS.Protect Password:=MasterPass, _
                DrawingObjects:=True, _
                Contents:=True, _
                Scenarios:=True, _
                AllowFormattingCells:=False, _
                AllowFormattingColumns:=False, _
                AllowFormattingRows:=False, _
                AllowInsertingColumns:=False, _
                AllowInsertingRows:=False, _
                AllowInsertingHyperlinks:=False, _
                AllowDeletingColumns:=False, _
                AllowDeletingRows:=False, _
                AllowSorting:=False, _
                AllowFiltering:=False, _
                AllowUsingPivotTables:=False
        Next WS
End Sub



Sub UnlockSheets()

For Each WS In ActiveWorkbook.Worksheets
            WS.Protect Password:=MasterPass, _
                DrawingObjects:=False, _
                Contents:=False, _
                Scenarios:=False, _
                AllowFormattingCells:=True, _
                AllowFormattingColumns:=True, _
                AllowFormattingRows:=True, _
                AllowInsertingColumns:=True, _
                AllowInsertingRows:=True, _
                AllowInsertingHyperlinks:=True, _
                AllowDeletingColumns:=True, _
                AllowDeletingRows:=True, _
                AllowSorting:=False, _
                AllowFiltering:=False, _
                AllowUsingPivotTables:=False
        Next WS

End Sub

There are two problems in your Unlock code as posted in your original post.您在原始帖子中发布的解锁代码中有两个问题。

Contents:=False

will override protecting the sheet and is why the worksheet remains unprotected after you run the macro.将覆盖保护工作表,这就是运行宏后工作表保持不受保护的原因。

AllowDelectingRows:=True

should have given a Compile error: Named argument not found.应该给出Compile error: Named argument not found. due to the typo: AllowDele**c**ting Rows I don't understand why it did not on your system.由于打字错误: AllowDele**c**ting Rows我不明白为什么它不在你的系统上。

Here is the modified code:这是修改后的代码:

======================================== ========================================

Option Explicit
Sub UnlockSheets()
Dim WS As Worksheet
For Each WS In ActiveWorkbook.Worksheets
            WS.Protect Password:=MasterPass, _
                DrawingObjects:=False, _
                Contents:=True, _
                Scenarios:=False, _
                AllowFormattingCells:=True, _
                AllowFormattingColumns:=True, _
                AllowFormattingRows:=True, _
                AllowInsertingColumns:=True, _
                AllowInsertingRows:=True, _
                AllowInsertingHyperlinks:=True, _
                AllowDeletingColumns:=True, _
                AllowDeletingRows:=True, _
                AllowSorting:=False, _
                AllowFiltering:=False, _
                AllowUsingPivotTables:=False
        Next WS

End Sub

======================================== ========================================

If you just want a working security for accidental misuse that happens in a workbook shared with others this should work in the Worksheet_Activate event如果您只想为与他人共享的工作簿中发生的意外误用提供工作安全性,这应该在 Worksheet_Activate 事件中起作用

Private Sub Worksheet_Activate()
 Dim WhoCanSort As String
  WhoCanSort = ThisWorkbook.WriteReservedBy
   If WhoCanSort = "skkakkar" Then
     ActiveSheet.Unprotect
    Else:
     ActiveSheet.Protect AllowSorting:=False
   End If 
End Sub

EDIT Protecting worksheet elements By default, when you protect a worksheet, all the cells on the worksheet are locked, and users cannot make any changes to a locked cell.编辑保护工作表元素默认情况下,当您保护工作表时,工作表上的所有单元格都被锁定,用户无法对锁定的单元格进行任何更改。 For example, they cannot insert, modify, delete, or format data in a locked cell.例如,他们不能在锁定的单元格中插入、修改、删除或格式化数据。 However, you can specify which elements users will be able to change when you protect the worksheet.但是,您可以指定在保护工作表时用户可以更改哪些元素。 Before you protect a worksheet, you can unlock the ranges that you want users to be able to change or enter data in. You can unlock cells for all users or for specific users.在保护工作表之前,您可以解锁希望用户能够更改或输入数据的范围。您可以解锁所有用户或特定用户的单元格。

Lock or unlock specific areas of a protected worksheet I think by following this method you may be able to prevent users from using any commands to sort data (Data tab, Sort & Filter group).Users can't sort ranges that contain locked cells on a protected worksheet, regardless of this setting. 锁定或解锁受保护工作表的特定区域我认为通过遵循此方法,您可以防止用户使用任何命令对数据进行排序(数据选项卡、排序和筛选组)。用户无法对包含锁定单元格的范围进行排序受保护的工作表,无论此设置如何。

Ok, here it is.好的,它来了。 I put this into the Workbook_open -event, as I think, this is the best place.我把它放到Workbook_open -event 中,因为我认为,这是最好的地方。

Private Sub Workbook_open()
ActiveSheet.Protect _
                AllowDeletingColumns:=True, _
                AllowDeletingRows:=True, _
                AllowFormattingCells:=True, _
                AllowFormattingRows:=True, _
                AllowFormattingColumns:=True, _
                AllowInsertingHyperlinks:=True, _
                AllowInsertingRows:=True, _
                AllowUsingPivotTables:=True, _
                AllowSorting:=False, _
                AllowFiltering:=False

ActiveSheet.Protection.AllowEditRanges.Add _
                Title:="yourtitle", _
                Range:=Range("yourrange")

End Sub

This allows everything, except sorting.这允许一切,除了排序。 All other properties are listed here 此处列出 所有其他属性

I know this post is old, but i got stuck with the same problem.我知道这篇文章很旧,但我遇到了同样的问题。 After searching throught all web i found a workaround to make this doable.在通过所有网络搜索后,我找到了一种解决方法来使此操作可行。 In case someone eventually have the same question here is the link to the "solution": https://techcommunity.microsoft.com/t5/excel/disabling-sort-functions-also-disable-grouping/mp/2596241如果有人最终有同样的问题,这里是“解决方案”的链接: https : //techcommunity.microsoft.com/t5/excel/disabling-sort-functions-also-disable-grouping/mp/2596241

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

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