寻求有关VBA用户限制的一些帮助。 到目前为止,我已将代码粘贴在下面。它工作得很好,但我希望以此为基础。
我拥有它,因此列出的特定用户可以访问该文件,尝试访问该文件的任何其他人都会msgbox
说他们未获得授权,然后该书就会关闭。
但我希望有些用户可以看到一些表格(他们不应该看到的表格是xlveryhidden)然后其他用户可以看到列出的其他表格...
即: 名称1可以看到表13, Name2可以看到sheet14和sheet3 名称3可以看到sheet22 sheet23和sheet4 在安全性方面,它并不是非常重要,它们都来自同一个团队,但只是为了用户友好和整洁的文档。
Private Sub Workbook_Open()
Dim Users As Variant
Dim UName As String
Dim UFind As Variant
Users = Array("Name1", "Name2", "Name3", "Name4", "Name5")
UName = Environ("UserName")
On Error Resume Next
UFind = WorksheetFunction.Match(UName, Users, 0)
If Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
答案 0 :(得分:1)
想出了一个答案,它非常简单,并且不会承受新用户的添加,但是平均时间还可以......
Private Sub Workbook_Open()
Dim Users As Variant
Dim UName As String
Dim UFind As Variant
Users = Array("Name1", "Name2", "Name3")
UName = Environ("UserName")
On Error Resume Next
UFind = WorksheetFunction.Match(UName, Users, 0)
If UName = "Name2" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
ElseIf UName = "Name1" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
Worksheets("Sheet4").Visible = True
ElseIf UName = "Name3" Then
Worksheets("Sheet23").Visible = True
Worksheets("SHEET17").Visible = True
ElseIf Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
为了在关闭文件时再次重新隐藏它们:
SubPrivate Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Sheet23").Visible = False
Worksheets("SHEET17").Visible = False
Worksheets("Sheet4").Visible = False
Worksheets("Sheet1").Visible = False
‘If you don’t save it’s not effective
Me.Save End Sub
答案 1 :(得分:0)
将If
条件更改为:
If Err <> 0 Then
MsgBox "You are not authorised to use this Workbook"
ThisWorkbook.Close SaveChanges:=False
Else
For Each ws In Worksheets
If ws.Name <> "Sheet" & UFind Then
ws.Visible = xlSheetHidden
End If
Next ws
End If
确保工作表名称为Sheet1,Sheet2,Sheet3,..等问题中提到的。