避免尝试有条件地保护共享工作簿中的单元格的变通办法

现在…我有一个工作簿5个选项卡。 在标签1(仪表板)上,我有四个button – 每个button基本上是其他四个之一的链接。 其中包含button的原因是只有具有“pipe理员”权限的用户才能访问并更改其他四个选项卡上的数据。

所有其他用户不应该被允许在其他四个选项卡上更改任何数据。 这是一个“看,但不碰”的政策。 噢 – 把它复杂一点,然后我们决定他们应该能够在每个工作表上改变一列。

所以。 这是一个共享的工作簿。 我的一段代码:

Private Sub cmdViewHistology_Click() If UserPermsLevel = "High" Or UserPermsLevel = "Super" Then Worksheets("Histology and Cytology").Visible = True Worksheets("Histology and Cytology").Activate Exit Sub ElseIf (UserPermsLevel = "Normal" Or UserPermsLevel = "Normal and UserName") Then Worksheets("Histology and Cytology").Visible = True Worksheets("Histology and Cytology").Range("A:I").Locked = True Worksheets("Histology and Cytology").Range("J:J").Locked = False Worksheets("Histology and Cytology").Protect DrawingObjects:=False, Contents:=True, Scenarios:=False Worksheets("Histology and Cytology").Activate Else MsgBox "Sorry, this command is not available." End If End Sub 

它不起作用。 因为它是共享的,我知道我无法保护表单 – 这是一个耻辱。 问题是…我需要那些pipe理员用户能够改变一切,我需要其他人能够看,但不能触摸!

所以…有没有人有一个替代方法,我可以用这个? 目前我的解决方法是禁用四个button,并给他们一个失败的消息,这是不是赢得了我的任何好处…

真的真的希望有人有一个好主意,这将帮助我走出这个漏洞!

感谢您

如果您知道pipe理员用户的Windowslogin名(或非pipe理员,取决于哪个),则可以使用Environ("Username")来检查已批准用户的列表。

在工作表的更改事件上,找出正在更改的单元格,如果不允许,请检查该特定用户是否有权这样做。

 Private Sub Worksheet_Change(ByVal Target As Range) Dim EditableRange As Range Dim Intersect As Range Set EditableRange = Range("A1") ' Set your range appropriately... Set Intersect = Application.Intersect(EditableRange, Target) If Intersect Is Nothing Then 'If this is not in the list of editable cells, then check for admin status If UserPermsLevel <> "High" And UserPermsLevel <> "Super" Then Application.EnableEvents = False Application.Undo 'Undo the edits the user made Application.EnableEvents = True MsgBox "Sorry, this command is not available." End If 'Implied else: allow change End If 'Implied else: allow change End Sub 

此外,您需要在每个“不可编辑”选项卡上设置此代码,或者将其放在一个单独的模块中,然后由Private Sub Worksheet_Change(ByVal Target As Range)每个选项卡上的Private Sub Worksheet_Change(ByVal Target As Range)调用该模块,而不是在代码上对于切换可见选项卡的button。

也许检查SelectionChange事件如果ActiveCell.Column是任何你想要允许和UnProtect表,任何其他列会断言保护。 保持东西,否则locking到所有,但pipe理员。 – 我的意思是build议@Gaffi检查UserPermsLevel也是一个好主意