Unprotect cell based on Username match

justme

Well-known Member
Joined
Aug 26, 2002
Messages
729
Code:
Sheets("sheet1").Range("a1").Value = Environ("username")
   
    If Range("a1").Value = Range("H5").Value Then format.cell("c24")Protection=unlocked


I will have someone type in their supervisor's name in H5. When they send the form to their supervisor and A1 matches the entry in H5, I want cell c24 to be unlocked so the supervisor can make an entry.

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
If Environ("username") = Range("H5") Then
    Range("C24").Locked = False
Else
    Range("C24").Locked = True
End If
 
Upvote 0
Hmmm problems.....

Is there a problem when you are using merge cells for the fields?

H5 is actually H5:j5 and c24 is actually c24:e24
 
Upvote 0
As a caution, if your sheet is protected, you will need to unprotect it in the code.
Code:
Activesheet.UnProtect "password"
If Environ("username") = Range("H5") Then 
    Range("C24").Locked = False 
Else 
    Range("C24").Locked = True 
End If 
ActiveSheet.Protect "password"

lenze
 
Upvote 0
Hmmm problems.....

Is there a problem when you are using merge cells for the fields?

H5 is actually H5:j5 and c24 is actually c24:e24

Apparently so!!! I get an Error with merged cells, as is common. VBA doesn't seem to understand them. IMO, merged cells should almost never be used.

Instead of merging your cells, format them as "Center Across Selection", particularly the ones to be unlocked (Format>Cells>Alignment>Horizonal Field). The code should now run.

lenze
 
Upvote 0
Edit - cells are not merged anymore!

Thanks Lenze, I thought that might be the problem (I did have the worksheet protected). So I put in your code. Then I thought, if the username = the supervisor's name I don't care what cell is unprotected. I can just leave the workbook unprotected and they can write anywhere. Then when the workbook gets mailed back to others, the whole thing is locked back up.

So my code looks like;
Sheets("sheet1").Range("a1").Value = Environ("username")

If Environ("username") = Range("H5") Then
ActiveSheet.Unprotect
Else
ActiveSheet.Protect
End If

End Sub

It isn't working. So, I tried pasting it everywhere thinking it might have to reside in ThisWorkbook or a Sheet module. So far no luck. Any more suggestions?

The way I am testing this is to type my name in the Supervisor cell and see if it will unlock the workbook, then type in any other name to see if it will lock.
 
Upvote 0
One or two ways

In the ThisWorkbook module
Code:
Private Sub Workbook_Open()
    If Sheet1.Range("$A$1") = Environ("UserName") Then
    Sheet1.Unprotect
    Else
    Sheet1.Protect
    End If
End Sub

In the WorkSheet module
Code:
Private Sub Worksheet_Activate()
    If Range("$A$1") = Environ("UserName") Then
    ActiveSheet.Unprotect
    Else
    ActiveSheet.Protect
    End If
End Sub

Both seem to work

lenze
 
Upvote 0
Lenze,
I used the first example and banged my head all afternoon til I figured out case sensitive. duh!!

it's working great. Thanks alot.
 
Upvote 0
Lenze,
I used the first example and banged my head all afternoon til I figured out case sensitive. duh!!

it's working great. Thanks alot.

Been there, done that :roll:

You can get around that in VBA by using the UCase method. See help files for examples.

Code:
If UCase(Range("$A$1") = UCase(Environ("UserName") Then

lenze
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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