Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Password cell and lock after

0

Hi all i was wondering if anybody could help i have a excel sheet but i need to password a cell to input data and then auto lock once they move out of the cell or ask for the password again if they want to change it.

now the problem is i have a about 20 users who need to have there own password when editing the cell i know this is a little odd as i know how to password a cell but i need this for validation perpose and i can not think how i can work around it 

the cell in qustion is D45

Thanks for any help 

Answer
Discuss

Answers

0
Selected Answer

Hi Saxon and welcome to the Forum

In the attached file, Sheet1 is all unlocked except for cell D45 (in yellow). The sheet is protected by the password Master Password but all cells can be selected.

When a cell is selected, it triggers the Worksheet_SelectionChange event which triggers the code below. (Note that I've firstly declared two variables MastPwd and Pwds, before the sub so they have a wider scope).

Private MastPwd As String, Pwds As Variant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Lcked As Range

' state the locked cells(s)
Set Lcked = Range("D45")

If Target.CountLarge = 1 And Not Intersect(Target, Lcked) Is Nothing Then
    ' single cell selected= locked cell(s) so get user password
    r = InputBox("Please enter your personal password before editing this cell", "Password required...", "****")
    If r <> vbNullString Then
        ' set up passwords
        Call SetPwds
        ' loop through password array...
        For n = LBound(Pwds) To UBound(Pwds)
            'unlock sheet if password found
            If Pwds(n) = r Then ActiveSheet.Unprotect (MastPwd)
        Next n
    End If

    Else
    ' not locked cell(s) so protect sheet
    ActiveSheet.Protect (MastPwd)
End If
End Sub

The line in bold above calls another sub, also behind sheet1:

Private Sub SetPwds()

' record the sheet password here:
MastPwd = "MasterPassword"
' record individual passwords here:
Pwds = [{"123", "abc", "doreyme", "xyz", "789"}]

End Sub

where you need to change the bits in bold to list your (new) sheet password and your users' passwords (however many- the other code will handle them).

If the user enters a recognised password, D45's value can be changed in Excel's formula bar. If not, the user is prevented from doing so and is told it's a protected cell.

Hope this works for you- if so, kindly mark this answer as Selected.

Happy New Year!

Discuss
0

hi thanks so much for the information i will have a look at this later today still recovering from new year :-) but looking at this it will do what I need am just just starting to learn macro and VBA and is a challenge.

wishing you and everyone a happy new year let's hope for a good one. 

Thanks 

Discuss

Discussion

Hi Saxon. Hope my answer gives you a workable solution (and you select my answer).

For future, please note that you should add comments about an answer in the Discussion section of the answer. Answers are reserved for solutions to Questions really. 

Don has several super tutorials on VBA- you'll find a section in the Tutorials page. Better still, consider his VBA course which gives a structured approach to the whole subject (and it's heavily discounted at present).

Happy New Year to you too (and everyone).
John_Ru (rep: 6152) Jan 1, '23 at 5:26 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login