Environ("UserName") changes from "User123" to "usera123"

KyleJackMorrison

Board Regular
Joined
Dec 3, 2013
Messages
107
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,
I have a code which unlocks a sheet based on usernames.


I've noticed that the code sometimes doesn't work because our Environ("UserName") changes from a Capital letter at the start to sometimes a lower case letter randomly.


For example,

"Morrisonk123" to "morrisonk123" This throws my code out and doesn't unlock the sheet as I have it set up to recognise the lowercase username.


Is there a way to get around this or do I have to put "Morrisonk123", "morrisonk123" just to be safe?

Code:
Sub xUnlock()
    Dim sUser As String
    Application.ScreenUpdating = False
    sUser = Environ("UserName")
    Select Case sUser
        Case "morrisonk102", "user1", "user2"
            Sheets("FOE Jan 19 - Dec 19").Unprotect Password:="11SQN4AMR"
            MsgBox "Welcome " & Application.UserName & " the FOE has now been unlocked for editing.", vbOKOnly, "Welcome"
        Case Else
            Sheets("FOE Jan 19 - Dec 19").Protect Password:="11SQN4AMR"
    End Select
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You could use the ucase function on the first letter in combination with the Mid function and then put the password back together
 
Last edited:
Upvote 0
You could use

Code:
Option Compare Text

at the top of the module or:

Code:
Select Case LCase(sUser)
 
Upvote 0
Could you please elaborate or give me an example please?
Code:
Sub xUnlock()
    Dim sUser As String[COLOR=#b22222], First_Letter1 As String, First_Letter2 As String[/COLOR]
    
    Application.ScreenUpdating = False
    
    sUser = Environ("UserName")
    
[COLOR=#ff0000]    First_Letter1 = Mid(sUser, 1, 1) [/COLOR][COLOR=#008000]'returns a 1 character string starting at the first letter[/COLOR][COLOR=#ff0000][/COLOR]
[COLOR=#ff0000]    [/COLOR]
[COLOR=#ff0000]    First_Letter2 = UCase(First_Letter1) [/COLOR][COLOR=#006400]'a new variable will hold the ucase version[/COLOR]
    
[COLOR=#ff0000]    sUser = WorksheetFunction.Substitute(sUser, First_Letter1, First_Letter2, 1) [/COLOR][COLOR=#006400]'replace the first letter[/COLOR][COLOR=#ff0000][/COLOR]
    
    Select Case sUser


        Case "morrisonk102", "user1", "user2"
        
            Sheets("FOE Jan 19 - Dec 19").Unprotect Password:="11SQN4AMR"
            
            MsgBox "Welcome " & Application.UserName & " the FOE has now been unlocked for editing.", vbOKOnly, "Welcome"
        
        Case Else
        
            Sheets("FOE Jan 19 - Dec 19").Protect Password:="11SQN4AMR"
            
    End Select
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Moshi why would you do all that when LCase (or UCase) works perfectly well to remove case sensitivity?
 
Upvote 0
Your right. I over thought it.
Code:
[COLOR=#333333]Sub xUnlock()[/COLOR]    Dim sUser As String
    
    Application.ScreenUpdating = False
    
    sUser = lcase(Environ("UserName"))
        
    Select Case sUser


        Case "morrisonk102", "user1", "user2"
        
            Sheets("FOE Jan 19 - Dec 19").Unprotect Password:="11SQN4AMR"
            
            MsgBox "Welcome " & Application.UserName & " the FOE has now been unlocked for editing.", vbOKOnly, "Welcome"
        
        Case Else
        
            Sheets("FOE Jan 19 - Dec 19").Protect Password:="11SQN4AMR"
            
    End Select
    
    Application.ScreenUpdating = True
     [COLOR=#333333]End Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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