Veryhidden Sheets based on Usernamei and Password

GLower0617

New Member
Joined
Oct 1, 2010
Messages
17
I found this code searching forums and I have a specific use for it and I am trying to get it to function. I have only made minor changes to it so that it recognized the user row to determine which sheets to unlock. The idea is to prompt for username and password and then unhide the sheets that that particular user has access to. It matches the username in the user name column and then unhides the sheets that are listed in the same row as the username. I have it fully functioning now except that it will not unhide the last sheet in the loop. If there is only 1 sheet that the user is allowed to have access to then it will not unhide it as it sees it as the last sheet in the row. Not sure why. Any help is appreciated. Below is the sheet and the code.

Gary

User Name
PasswordSheetSheetSheetSheetSheetSheetSheet
PlantMgr0000
Roster
LeadershipA-CrewB-CrewC-CrewD-CrewUserList
ProdSup1111RosterLeadershipA-CrewB-CrewC-CrewD-CrewUserList
QualSup2222
Roster
Quality
TeamLead13333
RosterA-Crew
TeamLead24444RosterB-Crew
TeamLead35555RosterC-Crew
TeamLead46666Roster
D-Crew

<colgroup><col><col><col><col><col span="5"></colgroup><tbody>
</tbody>

Code:
Private Sub Workbook_Open()
Dim Sh As Worksheet
Dim UserName As String
Dim Password As String
Dim ThisCell As Range
Dim UserRow As Long
Dim C As Long

For Each Sh In ThisWorkbook.Worksheets
    If Sh.Name <> "Roster" Then
        Sh.Visible = xlSheetVeryHidden
    End If
Next Sh

UserName = InputBox("Please enter Username")
Password = InputBox("Please enter password")

For Each ThisCell In Sheets("UserList").Range("A2:A" & Sheets("UserList").Range("A50000").End(xlUp).Row)
    If UCase(ThisCell.Value) = UCase(UserName) And UCase(ThisCell.Offset(, 1).Value) = UCase(Password) Then
    MsgBox "Access Granted"
        For C = 3 To 11
            UserRow = ThisCell.Row
            If ThisCell.Offset(, C).Value <> "" Then
            Sheets(Sheets("UserList").Cells([UserRow], C).Value).Visible = xlSheetVisible
            End If
        Next C
        Exit Sub
    End If
Next ThisCell

MsgBox "Access Denied"
ThisWorkbook.Close

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I assume the usernames and passwords are not case sensitive?

I'm not sure why the last sheet in the range isn't showing - I suggest streamlining the code, and removing ambiguities. These include adding a Lastrow definition, and clarifying any offsets:
**Lastrow**
Add in LastRow, use that in your code for improved performance:
Rich (BB code):
Dim Lastrow as Integer
Lastrow = Sheets("UserList").Range("A50000").End(xlUp).Row
Dim Range1 as range
Set Range1 = Sheets("UserList").Range("A2:A" & Lastrow)

For each ThisCell in Range1 

***Offsets***
I know offset(, 3) technically works, but any ambiguities sometimes cause ghosts in the machine. Change any Offset(, 3) to Offset(0, 3). (Same for other numbers where you just have comma.)

See if that helps?
 
Upvote 0
Thanks for the suggestion. I did make the changes you suggested to help clean up the code. I was able to make a workaround for the original problem. I simply added a blank worksheet and named it IntentionallyBlank. By adding it to the sheets that should be unhidden then it simply unhides everything except the last sheet which is IntentionallyBlank. Simple workaround. I would still like to understand why it does not unhide the last sheet.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top