+ Reply to Thread
Results 1 to 13 of 13

Macros for changing variables with password

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Macros for changing variables with password

    Hi,

    I want to make a macro where a password has to be entered for the macro to work.
    Each password would make available all individuals details of his/her contained in 4 separate worksheets.


    There would be 9 different passwords (Individuals)
    Macro works only if the password and individual match
    Through each password, the data in the 4 sheets would get filtered to show only data relevant to that particular user.

    For example:
    when the individual and password match then 4 macros for each worksheet could be clicked

    Please help.

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macros for changing variables with password

    How about something like this :

    Sub passwords()
    Dim password As String
    password = InputBox("Please type password", "Please type password", "Password")
    If UCase(password) = "JIM" Then GoTo jim
    If UCase(password) = "JACK" Then GoTo jack
    If UCase(password) = "JAMES" Then GoTo james
    If UCase(password) = "JOHN" Then GoTo john
    Exit Sub
    jim:
    jack:
    james:
    john:
    MsgBox "do stuff"
    End Sub

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Hi JasperD

    Thanks for this!

    But apparently i'm in way over my head.

    Do you have any links where i can learn VB and how to input this code you have recommended?

  4. #4
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Or alternatively if you dont mind, i would highly appreciate it if you could give me step by step instructions on how to enter this code as i barely understand VB.

  5. #5
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macros for changing variables with password

    Can you post your workbook, tell me what you want done, so I will implement it and put the explanation in the vba code? I think that would be most efficient :-)

  6. #6
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Hi All

    Essentially this is what should happen.

    I have 5 sales representatives and i want to filter (Filter the column) each worksheet based on the representatives data.

    I have 4 sheets with different data ( sheet 2, sheet 3 , sheet 4, sheet 5)
    each sheet has all the sales representatives data.

    What i want to happen is, the first sheet (main sheet) should give the option for the sales rep to enter his/her name and password.

    If the name and password match then it gives him/her the option of going into the other 4 sheets and viewing only his/her data.


    Any help is much appreciated.

    Thanks in advance.
    Attached Files Attached Files

  7. #7
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macros for changing variables with password

    Right click on the sheet1 tab, select "view code" and copy & paste below macro there.
    It's a worksheet change macro, that checks if you change either cell D6 (name) or cell D8 (password).

    The rest is pretty self explanatory (and I put some explanation in there) - see if you can figure it out

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'A "worksheet_change" macro automatically fires whenever a cell on the worksheet changes - that's why I needs to be on the worksheet and not in a module, cause it's worksheet dependant.
    
    Dim pass As String, name As String 'set the variables used - we'll be looking at the passwords and the names
    
    If Target = Range("D6") Then Range("D8").Value = "" 'if Range D6 changes, then remove the password from D8"
    
    If Target <> Range("D8") Then Exit Sub 'if the change occurs in ANY other cell than D8, just exit the sub and do nothing
    pass = UCase(Range("D8").Value) 'Since we only check for changes in range D8, that's where the password is - set the variable pass to hold the password, in upper case
    name = UCase(Range("D6").Value) 'We also set the variable name to hold the name as in cell D6, also in upper case
    
    Application.EnableEvents = False
    Target.Value = "*****" 'change the cell D8 so the password isn't shown anymore - we disable the events for this part, cause we're changing cell D8 and don't want the macro to fire again
    Application.EnableEvents = True
    
    'If the name and passwords match any of the allowed combinations, we continue
    If name = "ALEX" And pass = "ABC" Then GoTo continue
    If name = "JACK" And pass = "DEF" Then GoTo continue
    If name = "JAMES" And pass = "GHI" Then GoTo continue
    If name = "JOHN" And pass = "JKL" Then GoTo continue
    
    'Else (if there's no match) - the sheets with data are set to be hidden, so the user can't check them, then we exit the sub
    Sheet2.Visible = False
    Sheet3.Visible = False
    Sheet4.Visible = False
    Sheet5.Visible = False
    Exit Sub
    
    continue:
    'If we continue, we don't want excel to 'jump all over the place', so we turn off screenupdating for the rest of the macro
    Application.ScreenUpdating = False
    
    'Since there was a correct name & pass entered, sheet2 to 5 can be shown
    Sheet2.Visible = True
    Sheet3.Visible = True
    Sheet4.Visible = True
    Sheet5.Visible = True
    
    'Normally, sheet 2 to 5 are protected (so the user can't show other users data), with the password "password" - since we need to change things on the sheet, we unprotect it
    Sheet2.Unprotect Password:="password"
    'We check which is the bottom row in column L that holds data and hide row L1 until that row, basically hiding all data
    Sheet2.Range("L1:L" & Sheet2.Range("L65536").End(xlUp).Row).EntireRow.Hidden = True
    
    'Then we check every row from L1 to the row with the bottom data, to check if it either contains "Sales Rep Name" or the name as selected on sheet1. If there's a match, we unhide those rows. The rest of the rows stay hidden
    
    For Each cell In Sheet2.Range("L1:L" & Sheet2.Range("L65536").End(xlUp).Row)
    If cell.Value = "Sales Rep name" Then cell.EntireRow.Hidden = False
    If UCase(cell.Value) = name Then cell.EntireRow.Hidden = False
    Next cell
    
    'We protect the sheet again, so the user can't unhide other users data
    Sheet2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="password"
    
    'This process is repeated for the other sheets.
    
    Sheet3.Unprotect Password:="password"
    Sheet3.Range("L1:L" & Sheet3.Range("L65536").End(xlUp).Row).EntireRow.Hidden = True
    For Each cell In Sheet3.Range("L1:L" & Sheet3.Range("L65536").End(xlUp).Row)
    If cell.Value = "Sales Rep name" Then cell.EntireRow.Hidden = False
    If UCase(cell.Value) = name Then cell.EntireRow.Hidden = False
    Next cell
    Sheet3.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="password"
    
    Sheet4.Unprotect Password:="password"
    Sheet4.Range("L1:L" & Sheet4.Range("L65536").End(xlUp).Row).EntireRow.Hidden = True
    For Each cell In Sheet4.Range("L1:L" & Sheet4.Range("L65536").End(xlUp).Row)
    If cell.Value = "Sales Rep name" Then cell.EntireRow.Hidden = False
    If UCase(cell.Value) = name Then cell.EntireRow.Hidden = False
    Next cell
    Sheet4.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="password"
    
    Sheet5.Unprotect Password:="password"
    Sheet5.Range("L1:L" & Sheet5.Range("L65536").End(xlUp).Row).EntireRow.Hidden = True
    For Each cell In Sheet5.Range("L1:L" & Sheet5.Range("L65536").End(xlUp).Row)
    If cell.Value = "Sales Rep name" Then cell.EntireRow.Hidden = False
    If UCase(cell.Value) = name Then cell.EntireRow.Hidden = False
    Next cell
    Sheet5.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="password"
    
    'At the end of the macro, we allow the screen to update again. If we didn't, Excel would basically 'freeze' and become useless.
    Application.ScreenUpdating = True
    
    'That's it - easy, isn't it :)
    End Sub
    The code could be made smaller with a "Do until" loop and then use a variable to select the sheet, but since you're new to VBA that might be more daunting then necessary, I decided to keep the repeated steps in, so you can understand it more easily. If you want a bit of a challenge, consider to use this instead :

    Replace the entire code with this :
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pass As String, name As String, i as integer
    
    If Target = Range("D6") Then Range("D8").Value = "" 
    If Target <> Range("D8") Then Exit Sub 
    
    pass = UCase(Range("D8").Value) 
    name = UCase(Range("D6").Value) 
    
    Application.EnableEvents = False
    Target.Value = "*****" 
    Application.EnableEvents = True
    
    If name = "ALEX" And pass = "ABC" Then GoTo continue
    If name = "JACK" And pass = "DEF" Then GoTo continue
    If name = "JAMES" And pass = "GHI" Then GoTo continue
    If name = "JOHN" And pass = "JKL" Then GoTo continue
    
    Sheet2.Visible = False
    Sheet3.Visible = False
    Sheet4.Visible = False
    Sheet5.Visible = False
    Exit Sub
    
    continue:
    Application.ScreenUpdating = False
    
    i = 2
    Do Until i = 6
    Sheets(i).Visible = True
    Sheets(i).Unprotect Password:="password"
    Sheets(i).Range("L1:L" & Sheets(i).Range("L65536").End(xlUp).Row).EntireRow.Hidden = True
    For Each cell In Sheets(i).Range("L1:L" & Sheets(i).Range("L65536").End(xlUp).Row)
    If cell.Value = "Sales Rep name" Then cell.EntireRow.Hidden = False
    If UCase(cell.Value) = name Then cell.EntireRow.Hidden = False
    Next cell
    Sheets(i).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="password"
    i = i + 1
    Loop
    
    Application.ScreenUpdating = True
    End Sub

    For the buttons on your sheet to work well, change the button code to :
    If Sheets("Sheet2").Visible = True Then Sheets("Sheet2").Activate
    These macros are in module1 and not on the page - so make sure to change them there.
    Last edited by JasperD; 05-08-2013 at 04:26 AM.

  8. #8
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Hi Jasper

    How do i get it to run?
    As in, i have put the button codes but how does the worksheet code get executed.

    I have put in the code you recommended in the attached sheet for more understanding.

    Thanks again JasperD!
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Whoa!! I think Whoa is a n understatement!! Thanks JasperD!!
    I Shall try to execute this and let you know..
    Thanks again

  10. #10
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macros for changing variables with password

    Hi,

    I just downloaded and tried your file. It just works, doesn't it?
    A Worksheet_Change macro runs automatically when you make a change on your sheet (in your case, sheet 1).
    When I type a password in cell D8, it changes to ***** right away - if I type the right password, sheet tabs 2-5 are visible and the buttons work, if I type the wrong password, they are not.
    The button 'run' isn't needed anymore and doesn't do anything indeed.
    A list of passwords on the sheet is also not needed any more. Even the list with user names isn't, unless you want to keep working with a dropdown list.

    What the sheet doesn't do is give a message when a wrong username or password is entered. If you want to include this, change the code as follows :

    Put it in between here :

    If name = "JOHN" And pass = "JKL" Then GoTo continue
    
    
    MsgBox "Wrong combination of username and password. Data not visible. Please enter correct details."
    
    
    'Else (if there's no match) - the sheets with data are set to be hidden, so the user can't check them, then we exit the sub
    Sheet2.Visible = False
    If the sheet for you doesn't automatically do the things I said above (and you're sure you enabled macros), it might be that your excel bugged at event checking some time and it's halted now.
    Either close excel entirely and re-start it and open the file, or go to the VB editor (Control + F11), and press Control + G to open the "Direct" window.
    There type :
    Application.EnableEvents = True
    and press enter.

    Now go back to the sheet, it should work fine.

    Let me know if you have any other questions.
    Oh and please click the * below if this helps


    ps - one last thing ; if you are going to give this sheet to users, you'd do well to protect the VB code with a password ; otherwise any user can just check the VBA code to see usernames and passwords.
    Do this by in the VB editor go to the menu "Extras" on the top, select "VB properties" and go to tab "Protection" - there tick the box that says "lock the project from viewing" and enter a password below it.
    Also, if you want to protect the sheet from users checking other people's data, change this part of code :

    From :
    Sheet2.Visible = False
    Sheet3.Visible = False
    Sheet4.Visible = False
    Sheet5.Visible = False
    To:
    Sheet2.Visible = xlVeryHidden
    Sheet3.Visible = xlVeryHidden
    Sheet4.Visible = xlVeryHidden
    Sheet5.Visible = xlVeryHidden
    What's the difference? A sheet that's merely hidden (.visible = false) can be made unhidden by the users, by clicking on a visible tab sheet and say "unhide", where they can see and select hidden sheets to unhide.
    A sheet thats "visible = xlveryhidden" is invisible from within excel itself, can't be unhidden by the users and only be unhidden from a macro.

  11. #11
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Hi JasperD

    You're correct, the file works now.
    I added in the stuff you told me as well. Thanks heaps. You don't know how much time you will save me in the future.
    Thanks especially for taking time to write the code and then explaining it in layman terms for me.
    It really helped me understand the whole process. Highly appreciated!!

    Just one small issue-
    The msgbox for the wrong username/password combo fires even before the user enters the password.
    i.e By default if cell D6 is set to John and i were to change it to Jim, then even before entering the password for Jim the msgbox fires.

    Is there a workaround for this?

    Once again thanks so much for all the help!

  12. #12
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macros for changing variables with password

    Sorry, did it without verifying

    Make that line read

    If Target = Range("D8") Then MsgBox "blah blah"

    Then it only fires on wrong combination of username / pass after entering the pass

  13. #13
    Registered User
    Join Date
    05-07-2013
    Location
    paradise isle
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macros for changing variables with password

    Ha Ha no need to apologize!! you're the one helping me out

    But that still doesnt work, the same issue is there.

    Also if possible could you let me know how to enter a msgbox if the username /password combo is correct?

    only if its not too much of an inconvenience!!

    Much Thanks JasperD
    Last edited by mshaheenlk; 05-09-2013 at 02:49 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1