so a while back a very knowledgeable user DMT32 helped me update his macro to meet my needs and all of a sudden it started having issues. Works great for users defined as admin but everyone else gets a Subscript out of range error. The Macro does do what its supposed to do, but the error is a nuisance hope someone can help
The original post was here
https://www.mrexcel.com/forum/excel...hide-worksheets-based-windows-user-names.html
my code is here
any help would be appreciated
The original post was here
https://www.mrexcel.com/forum/excel...hide-worksheets-based-windows-user-names.html
my code is here
Code:
Private Sub Workbook_Open()
Dim Admin As Boolean
Dim msg As Variant
Dim LastCol As Integer, C As Integer
Dim rng As Range
Dim sh As Worksheet, Userlist As Worksheet
On Error GoTo myerror
ThisWorkbook.Sheets(HomeSheet).Visible = xlSheetVisible
HideSheets
Set Userlist = UserTable("User List")
With Userlist
.Unprotect Password:=shPassword
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rng = .Range("A2:A" & lastrow)
End With
'check valid user
If IsValidUser(rng, Admin) Then
Application.ScreenUpdating = False
'Admin User unhide all sheets
If Admin Then
For Each sh In ThisWorkbook.Worksheets
sh.Visible = xlSheetVisible
sh.Unprotect Password:=shPassword
Next sh
Else
'unhide user sheets
With Userlist
For C = 3 To LastCol
If UCase(.Cells(rng.Row, C).Value) = "X" Then
With Sheets(.Cells(1, C).Value)
.Visible = xlSheetVisible
'.Unprotect Password:=shPassword
End With
End If
Next C
'If Len(shPassword) > 0 Then .Protect Password:=shPassword
End With
End If
'activate home sheet
Worksheets(HomeSheet).Activate
Else
'user Not Valid
For C = 3 To LastCol
If UCase(Userlist.Cells(2, C).Value) = "X" Then
With Sheets(Userlist.Cells(1, C).Value)
.Visible = xlSheetVisible
.Unprotect Password:=shPassword
End With
End If
Next C
'activate home sheet
Worksheets(HomeSheet).Activate
If Len(shPassword) > 0 Then Userlist.Protect Password:=shPassword
End If
myerror:
Application.ScreenUpdating = True
If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
Sheets("sf1").Visible = Hidden
[Min_Margin].Value = 0.3
Set ws = Sheets("Detailed Proposal")
With Sheets("Detailed Proposal")
.Protect Password:="password", AllowFormattingCells:="true", AllowFormattingColumns:="true", AllowFormattingRows:="true"
End With
Set ws = Sheets("configuration")
With Sheets("configuration")
.Protect Password:="password", AllowFormattingCells:="true", AllowFormattingColumns:="true", AllowFormattingRows:="true"
End With
Set ws = Sheets("Implementation Questionaire")
With Sheets("Implementation Questionaire")
.Protect Password:="password", AllowFormattingCells:="true", AllowFormattingColumns:="true", AllowFormattingRows:="true"
End With
End Sub
any help would be appreciated
Last edited: