gpt4 book ai didi

vba - 避免尝试有条件地保护共享工作簿中的单元格的解决方法

转载 作者:行者123 更新时间:2023-12-05 01:05:21 26 4
gpt4 key购买 nike

现在...我有一个包含 5 个选项卡的工作簿。在选项卡 1(仪表板)上,我有四个按钮 - 每个按钮基本上都是指向其他四个按钮之一的链接。它带有按钮的原因是只有对工作簿具有“管理员”权限的用户才能去更改其他四个选项卡上的数据。

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

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

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

这是行不通的。因为它是共享的,所以我知道我无法保护工作表——这很遗憾。问题是...我需要那些管理员用户能够更改所有内容,我需要其他人只能看但不能摸!

所以...有没有人有其他方法可供我使用?目前我的解决方法是禁用这四个按钮并给它们一个故障消息,这并没有给我带来任何好处......

真的非常希望有人有一个好主意可以帮助我摆脱这个困境!

谢谢你

最佳答案

如果您知道管理员用户(或非管理员,以较容易的为准)的 Windows 登录名,您可以使用 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) 调用在每个选项卡上,而不是在切换可见选项卡的按钮代码上。

关于vba - 避免尝试有条件地保护共享工作簿中的单元格的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9637084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com