Hi
Is it possible to use windows validation for users to grant access to individual spreadsheets in a common workbook
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Windows validation
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Windows validation
- This topic has 52 replies, 4 voices, and was last updated 11 years, 8 months ago.
AuthorTopicViewing 20 reply threadsAuthorReplies-
Maudibe
AskWoody_MVPJuly 21, 2013 at 6:42 pm #1402432This is the closest I found on Windows Authentication. Tried it but it needs some tweaking
http://vbnet.mvps.org/index.html?code/network/acceptsecuritycontext.htm
There is an interesting hack at http://datapigtechnologies.com/blog/index.php/hack-windows-authentication-for-your-applications/ using permissions to a share point folder but I don’t see why this could not be applied with permissions to a shared network folder as well
HTH,
Maud -
RetiredGeek
AskWoody_MVPJuly 21, 2013 at 8:07 pm #1402442Phil,
I don’t know what you mean by Windows Validation but you can do it with a little VBA as long as each user has their own logon account.
You just need to check the Environment Variable: UserName then unprotect the appropriate sheet and make sure you protect it on close or reprotect all sheets on open. BTW: You could also accomplish this by giving each sheet a different password. HTH :cheers: -
RetiredGeek
AskWoody_MVPJuly 21, 2013 at 8:44 pm #1402444Phil,
Here is some sample code:
In a module:
Code:Option Explicit Sub Auto_Open() Dim zUserID As String zUserID = Environ("UserName") '*** Add a Case statement for each UserName and the appropriate Sheet '*** Unprotect command with Password - I'd recomment All Upper Case PWs Select Case zUserID Case "Bruce" Sheets("Sheet2").Unprotect "TEST" Case Else MsgBox "The User Named: " & zUserID & vbCrLf & _ "Is not Authorized to use this Workbook.", _ vbOKOnly + vbCritical, _ "UnAuthorized Access Attempt" Application.Quit End Select 'Case zUserId End Sub
In the Workbook Object:
Code:Option Explicit Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim zUserID As String zUserID = Environ("UserName") '*** Add a Case statement for each UserName and the appropriate Sheet '*** Protect command '*** Note: The password must be set manually once and the workbook saved! Select Case zUserID Case "Bruce" Sheets("Sheet2").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Select 'Case zUserId End Sub
Notes:
-
[*]In the attached sheet you’ll need to get into the VBA and change the UserName “Bruce” to your UserName before you can test it. Change it in BOTH places! In you production code you should a password on the VBA module and select the Hide Code option to keep the curious from finding your PWs.
[*]I placed the reprotect code in a Workbook_BeforeSave event since if they don’t save it nothing will have changed.
[*]Sheet1 is protected and the code does not unprotect it. Sheet2 will be unprotected when you change the UserNames in the code.HTH :cheers:
-
Bonriki
AskWoody Lounger -
Bonriki
AskWoody LoungerJuly 29, 2013 at 5:42 pm #1403710Retiree hi
I am having trouble with the code.
I have in the Auto_Open module this :
Sub Auto_Open()Dim zUserID As String
zUserID = Environ(“UserName”)
‘*** Add a Case statement for each UserName and the appropriate Sheet
‘*** Unprotect command with Password – I’d recomment All Upper Case PWs
‘Set Focus = Sheets(“Instructions”)Select Case zUserID
Case “pgc”
Sheets(“Phil Carter”).Unprotect “hradmin”
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Phil Carter”).Unprotect “hradmin”
Case “dmw”
Sheets(“Dian Wilkinson”).Visible = xlSheetVisible
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Dian Wilkinson”).Unprotect “hradmin”and in the Workbook this:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)Dim zUserID As String
zUserID = Environ(“UserName”)
‘*** Add a Case statement for each UserName and the appropriate Sheet
‘*** Protect command
‘*** Note: The password must be set manually once and the workbook saved!Select Case zUserID
Case “pgc”
Sheets(“Phil Carter”).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Case “dmw”
Sheets(“Dian Wilkinson”).Protect DrawingObjects:=True, Contents:=True, Scenarios:=TrueI also have this code in the workbook to hide all sheets except “Instructions”:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In Me.Worksheets
If ws.Name “Instructions” Then ws.Visible = xlSheetHidden
Next ws
Me.Save
End SubCan’t seem to get the zUserID recognised
-
zeddy
AskWoody_MVPJuly 30, 2013 at 3:21 am #1403768Hi Phil
Re:
Select Case zUserID
Case “pgc”
Sheets(“Phil Carter”).Unprotect “hradmin”
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Phil Carter”).Unprotect “hradmin”
Case “dmw”
Sheets(“Dian Wilkinson”).Visible = xlSheetVisible
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Dian Wilkinson”).Unprotect “hradmin”
…Perhaps it should be
Select Case zUserID
Case “pgc”
Sheets(“Phil Carter”).Visible = xlSheetVisible
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Phil Carter”).Unprotect “hradmin”
Case “dmw”
Sheets(“Dian Wilkinson”).Visible = xlSheetVisible
Sheets(“Instructions”).Visible = xlSheetVisible
Sheets(“Dian Wilkinson”).Unprotect “hradmin”zeddy
-
zeddy
AskWoody_MVPJuly 30, 2013 at 3:46 am #1403770Hi Phil
It seems you have a couple of files where you have fifty sheets or so, one for each person.
And you are trying to control access for each person.Maybe you should rethink this, and give each person their own workbook.
The advantages include
1. the person doesn’t need to load such a large file containing data from 49 others
2. the person could work with their own data without interfering with other users read-write access
3. you don’t need to use shared workbooks with all the complications that arise
4. it is easier to restrict access to a datafile for that person only (plus admin users of course)
5. it is easy to generate a new datafile for a new user rather than adding more sheets to existing files etcNaturally, you would probably need a method to combine ALL the users data for analysis etc., but this is relatively easy.
You would need to use a simple file-naming convention for the datafiles e.g. incorporating the login UserID.
And you would need a Merge Tool.see my replies in post
http://windowssecrets.com/forums/showthread//155757-Macro-for-fetching-data-from-dynamic-range-of-a-spreadsheet-to-another-spreadsheet
..for an example of combining datafiles for up to 100 users.If you are using Excel version prior to 2007, let me know.
zeddy
-
-
-
-
Maudibe
AskWoody_MVP -
RetiredGeek
AskWoody_MVPJuly 22, 2013 at 5:09 pm #1402574Phil,
That was by design to show the code worked. The password to Sheet1 is TEST1, original huh?
Glad it is what you needed. :cheers:
-
Bonriki
AskWoody Lounger
-
-
RetiredGeek
AskWoody_MVPJuly 22, 2013 at 7:05 pm #1402592Phil,
Hopefully the two workbooks use the same UserNames so you can copy that Case stmt!
Better yet you can set it up so that all the macros are in one workbook and that workbook opens the ones with the named sheets then you only have one code base to maintain. This is how I set up many of my projects. :cheers:
-
Bonriki
AskWoody Lounger -
RetiredGeek
AskWoody_MVPJuly 23, 2013 at 7:42 am #1402621Phil,
The basic setup is a Workbook with all the code (VBA) that runs a series of data workbooks which are all setup identically. I usually use this setup where there is data which is stored by year, e.g. restarts every year w/no need to look at the data across years. The user opens the master workbook which immediately opens a dialog to allow them to select the desired data workbook.
34454-Selector
I’ve actually developed a standard code workbook that has all the standard functions like selection dialog box as shown above, application specific menu, sort code, etc. It just takes a few customizations (usually setting variables) to adapt it to a new data setup. Of course you do have to rewrite the data specific code/menus but all the repetitive stuff is just reused and there is only one version of the code to maintain. When I make a change to one of the standard modules it’s just a matter of copying it into each project after it has been fully tested. HTH :cheers: -
RetiredGeek
AskWoody_MVPJuly 29, 2013 at 6:09 pm #1403712Phil,
I always force the userids to upper case then compare against upper case just to eliminate problems.
[noparse]zUserID = UCase(Environ(“UserName”))[/noparse]I think you mis-typed in the first line of Case “pgc” shouldn’t that be like the 1st line of Case “dmw”? :cheers:
If I may ask why are you
-
Bonriki
AskWoody LoungerJuly 29, 2013 at 6:28 pm #1403731Phil,
I always force the userids to upper case then compare against upper case just to eliminate problems.
[noparse]zUserID = UCase(Environ(“UserName”))[/noparse]I think you mis-typed in the first line of Case “pgc” shouldn’t that be like the 1st line of Case “dmw”? :cheers:
If I may ask why are you
You are right. Not sure why I changed it!
I still can’t get it to work, which is frustrating as I was hoping to roll it out today!!!
-
-
RetiredGeek
AskWoody_MVPJuly 29, 2013 at 8:48 pm #1403747Phil,
Could you possibly post a test workbook? :cheers:
-
Bonriki
AskWoody Lounger
-
-
RetiredGeek
AskWoody_MVPJuly 30, 2013 at 4:36 pm #1403873Phil,
Works fine on my machine! With two minor changes.
1. Seems that Naming the Module containing the Auto_Open() code Auto_Open confused Excel. I changed the module name to Main.
34508-ProjectProperties
2. Please note the change to the first Case. You need to check against Upper Case values since you’re converting the UID to uppercase.Code:Select Case zUserID Case "BRUCE" Sheets("Phil Carter").Visible = xlSheetVisible Sheets("Instructions").Visible = xlSheetVisible Sheets("Phil Carter").Unprotect "hradmin" Case "dmw" Sheets("Dian Wilkinson").Visible = xlSheetVisible Sheets("Instructions").Visible = xlSheetVisible Sheets("Dian Wilkinson").Unprotect "hradmin" Case "iea" Sheets("Irina Alexeichik").Visible = xlSheetVisible Sheets("Instructions").Visible = xlSheetVisible Sheets("Irina Alexeichik").Unprotect "hradmin"
HTH :cheers:
-
Bonriki
AskWoody LoungerJuly 30, 2013 at 5:20 pm #1403876Retiree
Thanks very much that fixed my access but now I have another problem that is network related. I need to get our IT guys give All Staff access to a restricted area on our network.Forgot about that when I started.
Anyway, thanks very much for your help with this. I am not sure I would have ever realised that Excel was getting confused!
-
-
RetiredGeek
AskWoody_MVPJuly 30, 2013 at 8:15 pm #1403898Phil,
I got to think about this from a manageability perspective and decided this mess needed to be simplified so here’s a better way IMHO.
Now this is just a test section but you’ll get the drift the nice part is you only have to maintain the array of users and the rest is automatic.
Code:Option Explicit Type tdCredentials zUID As String zUserName As String End Type Public zSelectedUser As String Sub Auto_Open() Dim bAuthorized As Boolean Dim iCntr As Integer Dim zUserID As String Dim zUsers(30) As tdCredentials zUsers(0).zUID = "BRUCE": zUsers(0).zUserName = "Phil Carter" zUsers(1).zUID = "DMW": zUsers(1).zUserName = "Dian Wilkinson" zUsers(2).zUID = "IEA": zUsers(2).zUserName = "Irina Alexeichik" zUsers(4).zUID = "RAA": zUsers(3).zUserName = "Rosemary Apperley" zUserID = UCase(Environ("UserName")) bAuthorized = False For iCntr = 0 To UBound(zUsers) - 1 If zUserID = zUsers(iCntr).zUID Then bAuthorized = True zSelectedUser = zUsers(iCntr).zUserName Sheets(zUsers(iCntr).zUserName).Visible = xlSheetVisible Sheets("Instructions").Visible = xlSheetVisible Sheets(zUsers(iCntr).zUserName).Unprotect "hradmin" End If Next iCntr End Sub
Code:Option Explicit Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim zUserID As String zUserID = UCase(Environ("UserName")) Sheets(zSelectedUser).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub
HTH :cheers:
-
Bonriki
AskWoody Lounger
-
-
RetiredGeek
AskWoody_MVPJuly 30, 2013 at 9:17 pm #1403904Phil,
I used to do something similar for a training database written entirely in Excel. Each Group taught several courses and then all groups had to be combined for the numbers at the Division Level. So I set up each group with its own course workbook (the structure was identical across Groups). Then when the Division Chief needed his numbers a Master sheet would be opened where the Auto Open code would go out and grab the data sheet from each group workbook and consolidate it into a single sheet. Pivot sheets & Graphs were used for the summaries along with some SumIf & DSum formulas. Worked quite well. It all depends on your needs and user work requirements. In this case only one person in each group did all the input so the way I set it up allowed all groups to update any time they wanted since they each had their own workbook. As always good preliminary analysis of the workflow and requirements followed by good design makes things easier in the long run. So you have two good solutions now it’s up to you to “Make the Call!” :cheers:
-
zeddy
AskWoody_MVP -
zeddy
AskWoody_MVPAugust 1, 2013 at 3:57 pm #1404356Hi Phil
OK, here’s my version. Unzip the file to a folder of your choice.
The file is zipped because you can’t post .xlsb files here.
When you unzip this file, you’ll notice a significant file size reduction compared to your uploaded file.My file works like this:
When the file is opened with macros enabled, the User’s login is checked (via an Excel formula) to see if it is in a hidden Team List of Users.
If the login ID is found in the list, the User’s name is ‘fetched’ from the Team List, and the corresponding data sheet for that User is unhidden for access by the User.
If the login ID is not found in this list, a message is displayed, and the file will be closed.
(To allow RG and others to check out this file, I have commented out that ‘close file’ vba line in the startup routine, which would actually close the file. You would need to uncomment that line yourself. For anyone checking this example, add your own login ID to the list!)
.
If the login ID is found, a check is made to see if there is a worksheet for the User. If there isn’t a sheet for the User, one will be created automatically from the hidden template sheet.If the User’s login ID is one of a set of Admin User’s (defined in the startup routine), then all sheets are unhidden.
For an Admin User, a [StaffList] sheet is available , allowing new Users to be added (up to 100 in this sample).
This [StaffList] sheet also summarizes the number of Training Hours for all Users for each Event Type. (You need to copy the formulas down – I left this for you to do deliberately). The formulas use Custom Functions (e.g. =fetchConferenceDays(xx) ) rather than using Excel’s INDIRECT function (since the latter will give an error when dealing with sheetnames containing an apostrophe e.g. like Glen O’Leary)
For an Admin User, the sheet [Lists] uses dynamic range names and automatic sorting, for dropdown lists used on the Users own sheets. I have removed all of the sheet code from each User’s sheet as I believe these are not appropriate or required.
If you have any questions, please ask. Let me know what you think. Personally, I still believe the better way is to have separate datafiles for the Users.
zeddy
-
Bonriki
AskWoody LoungerAugust 6, 2013 at 6:34 pm #1405292Richard hi
I have had time now to test
Why are references for Conference, Online and Seminar like this =fetchConferenceDays(F16) and yet the remaining are like this =fetchEventTypeDays($F16,K$15)?I have entered some data in Dian Wlikinsons sheet, which toals OK but doesn’t update the Staff List
-
-
-
-
Bonriki
AskWoody Lounger -
zeddy
AskWoody_MVPAugust 7, 2013 at 9:31 am #1405381Hi Phil
The ‘different references’ are to show you different methods for gettting the Total duration days, based on the eventType, for a specified Staffname (i.e. sheet).
For example, as in cell [J17] of my file [rz1-StaffTraingingRecords.xlsb] we can use this:
=SUMIF(INDIRECT(“‘”&$F17&”‘!$C:$C”),J$15,INDIRECT(“‘”&$F17&”‘!$F:$F”))..where F17 is the corresponding staffname (which is also the sheet name for that person), $C:$C refers to the Event Type column and $F:$F refers to the Duration (days) on the data sheets. But you can’t use this INDIRECT type of formula if your names (and therefore corresponding sheet names) have apostrophe’s e.g. as in Glen O’Leary.
So, using a custom UDF (User definedFunction) we can have
=fetchConferenceDays(staffname)
=fetchOnlineDays(staffname)
=fetchSeminarDays(staffname)
These custom functions define the event type directly in the vba code for the corresponding vba function ie.
zType = “Conference” ‘training event type
zType = “Online”
zType = “Seminar”So, a more efficient method is to define a custom function where you specify the staffname (i.e sheetname) as well as the Event Type, as in
=fetchEventTypeDays(staffname,event type)So we can use
=fetchEventTypeDays($F16,L$15)
(note use of the $ to ‘lock’ the column F (staffname column), and the $ to ‘lock’ the row 15 (which is the header row for the event type i.e has the description for the event type)So you can use =fetchEventTypeDays(staffname,event type) for all event types rather than define them separately.
Re: I have entered some data in Dian Wlikinsons sheet, which toals OK but doesn’t update the Staff List
>After the workbook is saved by Dian, and opened by an Admin User, the Staff List sheet will show the results.
If Dian (or any non-Admin User) opens the workbook, they will not see the [StaffList] sheet. They can adds entries, then save and close the workbook. When an Admin User then opens the workbook, the [StaffList] sheet will be made visible, and it will show the correct values.But, to allow you to see the results ‘on the fly’ after any User’s sheet is manually updated, I have amended the file to include
Application.CalculateFull
..whenever the sheet [StaffList] is activated, i.e when you switch back to the [StaffList] sheet. see attached .xlsm version of the file.The reason you didn’t see Dian’s values updated is because the UDF’s are, by default, non-Volatile functions, so will only update the value shown in its cell if it is re-Entered! (They will be updated when the workbook is opened).
A Volatile function is re-calculated every time the sheet is re-calculated and is dynamic in responce to changes on the sheet. If the sheet doesn’t recalc, it doesn’t matter if you have Application.Volatile or not, the UDF will not execute.
Using Volatile sparingly gives you two benefits. Your worksheet will recalculate faster because it’s only recalculating what it needs to. And UDF’s are notoriously slow compared to built-in functions.
I guess that will open up a stack of responses from the other Forum members!
zeddy
-
Bonriki
AskWoody LoungerAugust 7, 2013 at 3:00 pm #1405426Richard
Once again thanks very much.I have found that when I open the workbook I only get the “Instructions” sheet and my sheet showing. Not sure why as you have set me as Admin and an instruction for Admin access to show all sheets. I have checked all the code and cannot find find out why the “unhide command for Admin users” doesn’t work.
I notice you have changed the file extension to *.xlsm. Are you recommending changing from the binary workbook extension *.xlsb?
-
zeddy
AskWoody_MVPAugust 7, 2013 at 3:14 pm #1405427Hi Phil
..I can’t upload .xlsb files to this site, so had to use .xlsm instead.
I recommend using binary .xlsb, so you can use Save As etc. etc.
As data is added, .xlsb will be a smaller file in the long run.I will investigate the Admin issue and get back to you.
That will be tomorrow.zeddy
-
zeddy
AskWoody_MVPAugust 7, 2013 at 3:27 pm #1405430Hi Phil
See updated file, for fixed Admin issue.
the Startup routine had:
zUserID = UCase(Environ(“UserName”)) ‘fetch login ID..but later on, for Admin Users, it checked like this..
If zUserID = “pgc” Then ‘<< this should have been UPPERCASE
unhideAllSheets
End IfSo the fix was:
If zUserID = "PGC" Then '<< v1.03; check UPPERCASE login ID
unhideAllSheets
End IfShould work for you now.
zeddy
-
Bonriki
AskWoody LoungerAugust 20, 2013 at 2:51 pm #1408079Richard hi
I am having problems with this workbook. I distributed it to 3 staff and asked them to open and let me know what they saw. They all had all worksheets visible.I have opened your rz3….xlsm file above and got this VBA error:
34750-UserIDFailCan you let me know what is going on. As far as I can determine the code looks fine!
-
-
Bonriki
AskWoody Lounger
-
-
-
zeddy
AskWoody_MVP -
Bonriki
AskWoody LoungerAugust 20, 2013 at 3:54 pm #1408088Hi Richard
This was when I opened your file. Your name was in the loginIDcell;
34751-zeddyLoginWhen I open my file, my name is in the loginIDcell so that seems to work OK. I have run some tests and the true/false is working for sheetnames.
As I said in previous the main problem is all staff being able to view all worksheets
-
zeddy
AskWoody_MVPAugust 20, 2013 at 4:11 pm #1408093Hi Phil
Two things to try:
1. Before sending the file to your Users, run the macro hideAllSheets, then save the file, then send that to your Users.
This is so that, if they don’t enable macros, then the file will be opened in the default state with only [Instructions] sheet showing.
2. Make sure your Users are not opening the file directly from within Outlook, i.e. if you are sending the file via email, make sure they save the file to a folder location before opening the file. Opening an Excel file directly from an attachment can cause a vba error on any random line of code!zeddy
-
-
Bonriki
AskWoody Lounger -
Bonriki
AskWoody LoungerAugust 28, 2013 at 5:27 pm #1409386Richard hi again
I have come across an unexpected problem. The Area Managers need to see the sheets of their groups.
I have tried with this code;
If zUserId = WLR Then
Sheets(“Marc Ferguson”).Visible = True
Sheets(“Greg Williams”).Visible = True
Sheets(“Geoff Hallam”).Visible = True
Sheets(“Dian Wilkinson”).Visible = True
Sheets(“Phil Carter”).Visible = True
ElseIf zUserId = “BRA” Then
Sheets(“Anne Hofstra”).Visible = True
Sheets(“Keith Towl”).Visible = True
Sheets(“Gavin Tasker”).Visible = True
Sheets(“Jane Busby”).Visible = True
Sheets(“Shelli Turner”).Visible = True
Sheets(“Adrienne Woollard”).Visible = True
Sheets(“Jen Ferguson”).Visible = Truebut all I get is the sheet at zUserID which is correct but ignores the rest!
-
zeddy
AskWoody_MVPAugust 29, 2013 at 8:03 am #1409454Hi Phil
..perhaps it’s a typo in your code..
Try this:Code:'********************************************** 'CHECK FOR AREA MANAGER'S.. '********************************************** On Error Resume Next If zUserId = "WLR" Then Sheets("Marc Ferguson").Visible = True Sheets("Greg Williams").Visible = True Sheets("Geoff Hallam").Visible = True Sheets("Dian Wilkinson").Visible = True Sheets("Phil Carter").Visible = True End If '---------------------------------------------- If zUserId = "BRA" Then Sheets("Anne Hofstra").Visible = True Sheets("Keith Towl").Visible = True Sheets("Gavin Tasker").Visible = True Sheets("Jane Busby").Visible = True Sheets("Shelli Turner").Visible = True Sheets("Adrienne Woollard").Visible = True Sheets("Jen Ferguson").Visible = True End If '---------------------------------------------- On Error GoTo 0
Let me know if this works for you.
Perhaps another way would be to colour-code the tabs for the different groups.
Then you could use something like:
If zUserId = “WLR” Then
unhideBrownSheets
End ifIf zUserId = “BRA” Then
unhidePurpleSheets
End if..see the routine Sub hideAllREDSheets() for example code etc etc.
zeddy
-
Bonriki
AskWoody Lounger -
zeddy
AskWoody_MVPAugust 30, 2013 at 3:30 am #1409521Hi Phil
We can fix this, but would need to see the exact code you have.
If you could make a copy of the workbook, delete most of the data and/or sheets to preserve the innocent, then post it back here, we could identify the problem, fix it, and tell you how to put the fix into your ‘live’ copy.
It might be as simple as one of the sheets you are referring to doesn’t exist, e.g. Sheets(“Adrienne Woollard”)zeddy
zeddy -
zeddy
AskWoody_MVPAugust 30, 2013 at 3:39 am #1409523Hi Phil
..just to let you know, the code for Sub hideAllREDSheets() was in the vba code module named [modHide]
..you can see the module names in the vbe window (Alt [F11] ), Project Explorer (Ctrl [R] )
But here is an example of how to use tab colours in vba code:Code:For Each zSht In ThisWorkbook.Worksheets 'loop through all worksheets '**************************************************** 'RUN FOLLOWING ROUTINE IF SHEET TAB COLOUR IS GREEN.. '**************************************************** If zSht.Tab.Color = RGB(0, 100, 0) Then 'RGB code for green tab 'do this End If '**************************************************** 'RUN FOLLOWING ROUTINE IF SHEET TAB COLOUR IS BLUE.. '**************************************************** If zSht.Tab.Color = RGB(0, 0, 100) Then 'RGB code for blue tab 'enter name of subroutine for Blue tab sheets here.. End If '**************************************************** Next zSheet 'process next worksheet
zeddy
-
Bonriki
AskWoody Lounger
-
-
-
-
-
RetiredGeek
AskWoody_MVPSeptember 1, 2013 at 2:05 pm #1409730Phil,
You’re missing a lot of quotes in the UserIDs:
Code:On Error Resume Next If zUserId = WLR Then Sheets("Marc Ferguson").Visible = True Sheets("Greg Williams").Visible = True Sheets("Geoff Hallam").Visible = True Sheets("Dian Wilkinson").Visible = True Sheets("Phil Carter").Visible = True End If If zUserId = "BRA" Then Sheets("Anne Hofstra").Visible = True Sheets("Keith Towl").Visible = True Sheets("Gavin Tasker").Visible = True Sheets("Jane Busby").Visible = True Sheets("Shelli Turner").Visible = True Sheets("Adrienne Woollard").Visible = True Sheets("Jen Ferguson").Visible = True End If If zUserId = DMW Then Sheets("Kay Brown").Visible = True Sheets("Claire Moon").Visible = True Sheets("Nisha Yakub").Visible = True End If If zUserId = GDO Then Sheets("Tejinder Singh").Visible = True Sheets("Paras Kumar").Visible = True End If If zUserId = "AJH" Then Sheets("Don Arnold").Visible = True Sheets("Trevor Moran").Visible = True Sheets("Robert Glass").Visible = True Sheets("Jed Gibson").Visible = True Sheets("Ian Laban").Visible = True Sheets("Michael Vercoe").Visible = True Sheets("Olento Peauafi").Visible = True End If If zUserId = KRT Then Sheets("Paul Nichols").Visible = True End If If zUserId = GJT Then Sheets("Claire Jongenelen").Visible = True End If If zUserId = JKF Then Sheets("Rogini Royeppen").Visible = True Sheets("Erin Forrest").Visible = True End If If zUserId = JLB Then Sheets("Sharon Thomas").Visible = True End If If zUserId = SJT Then Sheets("Erin Retter").Visible = True Sheets("Shiva Sami").Visible = True Sheets("Gopy Sundararajah").Visible = True End If If zUserId = ADW Then Sheets("David Sidwell").Visible = True Sheets("Ruth Porter").Visible = True Sheets("Carolyn Osborne").Visible = True End If
All of the 3 character USERID’s need to be quoted. HTH :cheers:
-
RetiredGeek
AskWoody_MVPSeptember 1, 2013 at 2:27 pm #1409733Phil,
It took me a while to hack in so I could add myself to the StaffList but I got there then tested this code for the Startup()
Code:'***********************************************'******************************************* ' Staff Training Tool v1.00 AUG 2013 TOP OF MODULE ' ' FILE : [StaffTrainingRecords.xlsb] last updated: 31-JUL-2013 by: RZ '***********************************************'******************************************* Sub startUp() 'v1.00 Application.Calculation = xlCalculationAutomatic 'make sure calcs are ON zUserId = UCase(Environ("UserName")) 'fetch login ID [loginIDcell] = zUserId 'place loginID in named cell '********************************************** 'CHECK IF LOGIN ID IS IN STAFF LIST.. '********************************************** zSheet = [staffSheetname] 'fetch computed value from named cell If zSheet = "" Then 'this User's loginID is NOT in Staff List 'display message.. saywhat = "The LoginID: " & zUserId & " is not listed as " & vbCr 'message text saywhat = saywhat & "an Authorized user of this workbook." 'add text saywhat = saywhat & vbCr & vbCr 'add 2 lines saywhat = saywhat & "Please check with your Administrator." 'add text saywhat = saywhat & vbCr & vbCr 'add 2 lines btns = vbOKOnly + vbCritical 'message box buttons boxtitle = "UnAuthorized Access Attempt" 'message box heading answer = MsgBox(saywhat, btns, boxtitle) 'display message box ThisWorkbook.Close savechanges:=False 'close THIS workbook Exit Sub 'nothing else to do End If 'end if test for missing loginID in List '********************************************** 'UNHIDE USER'S SHEET.. '********************************************** On Error GoTo createSheet 'set error trap for next line Sheets(zSheet).Visible = True 'unhide User's sheet On Error GoTo 0 'reset error trap '********************************************** 'UNLOCK USER'S SHEET.. '********************************************** Sheets(zSheet).Unprotect "hradmin" '********************************************** 'CHECK FOR ADMIN USER.. '********************************************** 'you can add as many Admin users here as wanted 'Or "BRA" Or "AJH" Or "KRT" Or "GJT" Or "JLB" Or "SJT" Or "ADW" Or "JKF" Or "WLR" Or "MDF" Or "GDO" Or "DMW" '********************************************** 'CHECK FOR AREA MANAGER'S.. '********************************************** On Error Resume Next Select Case zUserId Case "WLR" Sheets("Marc Ferguson").Visible = True Sheets("Greg Williams").Visible = True Sheets("Geoff Hallam").Visible = True Sheets("Dian Wilkinson").Visible = True Sheets("Phil Carter").Visible = True Case "BRA" Sheets("Anne Hofstra").Visible = True Sheets("Keith Towl").Visible = True Sheets("Gavin Tasker").Visible = True Sheets("Jane Busby").Visible = True Sheets("Shelli Turner").Visible = True Sheets("Adrienne Woollard").Visible = True Sheets("Jen Ferguson").Visible = True Case "DMW" Sheets("Kay Brown").Visible = True Sheets("Claire Moon").Visible = True Sheets("Nisha Yakub").Visible = True Case "GDO" Sheets("Tejinder Singh").Visible = True Sheets("Paras Kumar").Visible = True Case "AJH" Sheets("Don Arnold").Visible = True Sheets("Trevor Moran").Visible = True Sheets("Robert Glass").Visible = True Sheets("Jed Gibson").Visible = True Sheets("Ian Laban").Visible = True Sheets("Michael Vercoe").Visible = True Sheets("Olento Peauafi").Visible = True Case "KRT" Sheets("Paul Nichols").Visible = True Case "GJT" Sheets("Claire Jongenelen").Visible = True Case "JKF" Sheets("Rogini Royeppen").Visible = True Sheets("Erin Forrest").Visible = True Case "JLB" Sheets("Sharon Thomas").Visible = True Case "SJT" Sheets("Erin Retter").Visible = True Sheets("Shiva Sami").Visible = True Sheets("Gopy Sundararajah").Visible = True Case "ADW" Sheets("David Sidwell").Visible = True Sheets("Ruth Porter").Visible = True Sheets("Carolyn Osborne").Visible = True Case "RICHARD", "HRADMIN", "PGC", "BRUCE" '<<REMOVE THIS OR CHANGE AS APPROPRIATE unhideAllSheets End Select '********************************************** Exit Sub 'all done; nothing else to do '------------------------------------------------------------ 'vba continues here if the User's login ID is found in the 'Staff List, but no sheet exists for this authorized User.. createSheet: Sheets("Blank Staff").Visible = True 'unhide sheet before copying it Sheets("Blank Staff").Copy after:=Sheets("Instructions") 'copy template sheet ActiveSheet.Name = zSheet 'and rename it with User name Sheets("Blank Staff").Visible = xlVeryHidden 'hide sheet after copying Resume Next 'continue processing above End Sub 'StartUp() '***********************************************'*******************************************
It works just fine and is much more efficient than repeated IF tests.
I used this modification for a test:
Code:Case "ADW", "BRUCE" Sheets("David Sidwell").Visible = True Sheets("Ruth Porter").Visible = True Sheets("Carolyn Osborne").Visible = True
Results:
34833-PhilTest
HTH :cheers: -
Bonriki
AskWoody Lounger -
zeddy
AskWoody_MVPSeptember 1, 2013 at 3:10 pm #1409740Hi RG
Top marks for identifying and resolving the issue.
(I was away – you got there before me)Phil:
As RG says, you must ‘wrap’ each of the zUserId’s being checked, in double quotes, like this “WLR”
If you don’t, vba assumes you are checking against a variable, e.g. a variable named WLR, which is empty.And as RG says, with so many Area Managers, it is probably better to use the vba Case command rather than so many If .. End If checks (although it is perfectly acceptable to do that).
I have attached an updated copy of your submitted file.
(It is a zip file containing the .xlsb file)zeddy
-
zeddy
AskWoody_MVP -
Bonriki
AskWoody Lounger
-
-
RetiredGeek
AskWoody_MVPSeptember 1, 2013 at 4:54 pm #1409755As RG says, you must ‘wrap’ each of the zUserId’s being checked, in double quotes, like this “WLR”
If you don’t, vba assumes you are checking against a variable, e.g. a variable named WLR, which is empty.
zeddyZeddy,
That’s why I always like to DIM all my variables and use an Option Explicit command at the top. The VBE would then have caught those unquoted strings as undeclared variables…I’m just sayin’ :cheers:
-
zeddy
AskWoody_MVPSeptember 2, 2013 at 3:52 am #1409776Hi RG
You are right. Option Explicit would have caught that.
I don’t mind admitting when I’m wrong.
I’m proud of my humility.
..but I can’t help thinking that Dimming your variables is like making an artist choose all the colours first before he starts a painting. Or choosing all your words before writing a story. Or selecting a cast before you’ve read the film script. Or..
OK, you win this time.zeddy
-
-
-
Viewing 20 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Two blank icons
by
CR2
5 hours, 50 minutes ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
8 hours, 15 minutes ago -
End of 10
by
Alex5723
10 hours, 55 minutes ago -
End Of 10 : Move to Linux
by
Alex5723
11 hours, 24 minutes ago -
Single account cannot access printer’s automatic duplex functionality
by
Bruce
7 hours, 53 minutes ago -
test post
by
gtd12345
16 hours, 56 minutes ago -
Privacy and the Real ID
by
Susan Bradley
7 hours, 3 minutes ago -
MS-DEFCON 2: Deferring that upgrade
by
Susan Bradley
7 hours, 53 minutes ago -
Cant log on to oldergeeks.Com
by
WSJonharnew
21 hours, 17 minutes ago -
Upgrading from Win 10
by
WSjcgc50
8 hours, 42 minutes ago -
USB webcam / microphone missing after KB5050009 update
by
WSlloydkuhnle
16 hours, 23 minutes ago -
TeleMessage, a modified Signal clone used by US government has been hacked
by
Alex5723
1 day, 12 hours ago -
The story of Windows Longhorn
by
Cybertooth
1 day ago -
Red x next to folder on OneDrive iPadOS
by
dmt_3904
1 day, 14 hours ago -
Are manuals extinct?
by
Susan Bradley
2 hours, 23 minutes ago -
Canonical ditching Sudo for Rust Sudo -rs starting with Ubuntu
by
Alex5723
2 days ago -
Network Issue
by
Casey H
1 day, 11 hours ago -
Fedora Linux is now an official WSL distro
by
Alex5723
2 days, 12 hours ago -
May 2025 Office non-Security updates
by
PKCano
2 days, 12 hours ago -
Windows 10 filehistory including onedrive folder
by
Steve Bondy
2 days, 14 hours ago -
pages print on restart (Win 11 23H2)
by
cyraxote
1 day, 15 hours ago -
Windows 11 Insider Preview build 26200.5581 released to DEV
by
joep517
2 days, 16 hours ago -
Windows 11 Insider Preview build 26120.3950 (24H2) released to BETA
by
joep517
2 days, 16 hours ago -
Proton to drop prices after ruling against “Apple tax”
by
Cybertooth
2 days, 23 hours ago -
24H2 Installer – don’t see Option for non destructive install
by
JP
1 day, 16 hours ago -
Asking Again here (New User and Fast change only backups)
by
thymej
3 days, 11 hours ago -
How much I spent on the Mac mini
by
Will Fastie
18 hours, 45 minutes ago -
How to get rid of Copilot in Microsoft 365
by
Lance Whitney
1 day, 14 hours ago -
Spring cleanup — 2025
by
Deanna McElveen
3 days, 17 hours ago -
Setting up Windows 11
by
Susan Bradley
2 days, 12 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.