test username on workbook open unlock all sheets

ADAMC

Well-known Member
Joined
Mar 20, 2007
Messages
1,169
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I have a rough idea of what I want but my structure is confusing me big time

Mixing if/ then/ else with For each/ next aaaaaaarghhhhhhhh

what i want to do:
IF username is "me" unlock every sheet in the workbook and show a msgbox
if its anyone but me leave it as is and just open it

what I have

Code:
Private Sub Workbook_Open()
If Environ("Username") = "EMPLOYEENAME" Then
'Unlock all the worksheets 
   MsgBox "Logged in as an admin worksheet fully accessible"
Else
'Just open as normal with sheet protection active
End If
End Sub

Can someone help me with the unprtect all worksheets bit?

I was along the lines of:

Code:
For Each wSheet In Worksheets 
        wSheet.Unprotect Password:= "" 
    Next wSheet

but couldnt seem to incorporate the 2 together.

On the workbook close event again id need to re test to say if being closed by the admin re - protect all worksheets again.......

Thanks :)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try like this

Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
If Environ("Username") = "EMPLOYEENAME" Then
'Unlock all the worksheets
   MsgBox "Logged in as an admin worksheet fully accessible"
   For Each ws In Me.Worksheets
        ws.Unprotect Password:="abc"
    Next ws
Else
'Just open as normal with sheet protection active
End If
End Sub
 
Upvote 0
VOG - Thank you...I guess I was almost there...........(Apologies)

I assume I can then just replicate on the close event:

Code:
Private Sub Workbook_Close()
Dim ws As Worksheet
If Environ("Username") = "EMPLOYEENAME" Then
'Unlock all the worksheets
   MsgBox "Logged in as an admin worksheet fully accessible"
   For Each ws In Me.Worksheets
        ws.Protect Password:="abc"
    Next ws
Else
'Just open as normal with sheet protection active
End If
End Sub


Also would you happen to know any good links where I can learn about code structure/ best practice.

Would like to try an learn about:
Select case
With end With
etc......


Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

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