P
Patrick Molloy
drop the GOTO - its unnecesary
Also you unprotect the sheet before the IF statement.
drop this too. Your IF handles protection.
now this line:
If username = "ABC" Or "CDE" Or "EFG" Or "GHI" Then
evaluates incorrectly.
If (username = "ABC") Or (username ="CDE") Or (username
="EFG") Or (username ="GHI") Then
This is clearly a case where a SELECT CASE staement would
be so much clearer...
Sub auto_open()
On Error Resume Next
Dim username As String
username = GetUserName
ActiveWorkbook.BuiltinDocumentProperties("Author") = _
username
SELECT CASE Username
CASE "ABC","CDE","EFG","GHI"
WITH Worksheets("Sheet1")
.Unprotect
.Cells.Locked = True
.Range("a3").Select
END WITH
CASE ELSE
ActiveSheet.Protect Contents:=True
END SELECT
END SUB
Note that you test for those that CAN edit the sheet. I'd
assume that nobody else can. In you IF what happens if a
username doesn't fall into EITHER category... you've
unprotected the sheet, so you're giving access. In my
code, if a user can edit, then the sheet is unprotected,
it is protected for everyone else.
think clearly about logic. use a diagram if it helps.
with IF statements, there's no need for GOTO statements.
in fact, there's almost no reason to use goto's except
for error handling. (let's not debate inferred goto's ok?)
Patrick Molloy
Microsoft Excel MVP
Also you unprotect the sheet before the IF statement.
drop this too. Your IF handles protection.
now this line:
If username = "ABC" Or "CDE" Or "EFG" Or "GHI" Then
evaluates incorrectly.
If (username = "ABC") Or (username ="CDE") Or (username
="EFG") Or (username ="GHI") Then
This is clearly a case where a SELECT CASE staement would
be so much clearer...
Sub auto_open()
On Error Resume Next
Dim username As String
username = GetUserName
ActiveWorkbook.BuiltinDocumentProperties("Author") = _
username
SELECT CASE Username
CASE "ABC","CDE","EFG","GHI"
WITH Worksheets("Sheet1")
.Unprotect
.Cells.Locked = True
.Range("a3").Select
END WITH
CASE ELSE
ActiveSheet.Protect Contents:=True
END SELECT
END SUB
Note that you test for those that CAN edit the sheet. I'd
assume that nobody else can. In you IF what happens if a
username doesn't fall into EITHER category... you've
unprotected the sheet, so you're giving access. In my
code, if a user can edit, then the sheet is unprotected,
it is protected for everyone else.
think clearly about logic. use a diagram if it helps.
with IF statements, there's no need for GOTO statements.
in fact, there's almost no reason to use goto's except
for error handling. (let's not debate inferred goto's ok?)
Patrick Molloy
Microsoft Excel MVP