+ Reply to Thread
Results 1 to 9 of 9

VBA to set password based on cell value

  1. #1
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    VBA to set password based on cell value

    TM - - Thank you for helping me with this, your suggestions on codes, etc worked out very well. I took the code that you suggested and modified it to meet my needs. I'm pasting in what I call a completed VBA code. This Code does the following:

    1) Adds all excel files found in a specific folder to a variable called ColFiles
    2) Opens each workbook one at a time
    3) While the first book is open it searches for Sheet2 and Sheet3 and if found deletes them, if not found it moves on
    4) Checks the value in cell AP11
    5) Based on that value - use a case statement to assign a password
    6) Saves the workbook in the same folder but with this new password.
    7) Repeats Step 2 - 7 until the list is complete.

    I think that is about it! Thanks again, smart dude - pushed me in the correct direction!!

    Please Login or Register  to view this content.
    Hello -- I have a bit of a problem with the code below. I attempting to set the password for a group of excel workbooks based on the value found in AQ11. The code runs through without error but when I go to the excel files after the files are saved I am not prompted to open them with the new password, the files open without the password. Can someone take a look and point me in the right direction? I put in red the reference to the cell value. Thanks you.

    Please Login or Register  to view this content.
    Last edited by Ironman; 09-21-2012 at 02:38 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,522

    Re: VBA to set password based on cell value

    You've not qualified the range with the sheet reference so I guess it will be using the Active Sheet. What's in AQ11 on the Active Sheet?

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to set password based on cell value

    Thanks TMShucks with your quick response. Column AQ will be a hidden column and the value in AQ11 will be the password I'd want to use, such as bt2 or hr2 or finance7...etc. This file will also be locked down from users making most changes...a few columns will be available for them to edit but for the most part they will not be able to modify it. I have that part all set up and ready to go, I'm just trying now to set passwords to open...do you have suggestions on how to qualify the range? Thanks again!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,522

    Re: VBA to set password based on cell value

    I'm thinking ...

    Please Login or Register  to view this content.

    You probably need to put a STOP at the beginning of the code and then step through it and see what the values are.


    Regards, TMS

  5. #5
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to set password based on cell value

    Thanks again TM...! The sheet won't be hidden, just a column. Maybe I need a suggestion. Here is basically my task:
    1) I'll have one folder that will contain about 50 workbooks.
    2) I want to use the code I posted earlier to open these 50 workbooks and saveas with a unique password.
    3) Perhaps some kind of CASE Statement that might read cell AQ11 (which contains a Business Unit name) and then assign the unique password..? Suggestions?

    Something like:

    Please Login or Register  to view this content.
    Then in the save as portion it might have the DIM of the case statement...

    Please Login or Register  to view this content.
    I just am not sure exactly how the code would work...thoughts on this idea? Does it make sense? Again, I do appreciate your efforts on this...this excel forumn is a great source!

  6. #6
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to set password based on cell value

    TM - - I started playing around with CASE code and the code below is not quiet right. I've saved the VBA code in one workbook saved as VBApwCode.xls. What I did find out in my testing was that if I added a value in workbook VBApwCode.xls in cell AQ11 the text: Marketing, then this part of the code works. But I need it to read the value in the workbooks I'm wanting to open then saveas then close...make sense? How do I can the code below to read the workbooks it's opening then saveas then closing?

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to set password based on cell value

    Hi TM (and all you other vba-gurus). I've taken the beginning code above and modified it to what I want...almost.

    To recap - - I have a folder that contains 50 workbooks. I want the VBA code to open each of these workbooks, check the value of cell AQ11 and using a Select Case statement assign a password based off that cell value. The code below works, but what I'm wondering if any of you might have thoughts on how to stream line the code. I have some VBA skills but nothing that matches the stuff you all can produce! Anyway, I'm sure there many ways to improve this code...so all suggestions welcome! The below code opens and saves 3 workbooks. Again, there are about 50 so I would like something that completes the task in a more stream line fashion.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,522

    Re: VBA to set password based on cell value

    You really need to loop through all the files in the folder and pass the name to your routine. The routine should open each file in the list, get the relevant cell, format the name, rename the file and then close it.

    See this example for getting a list of files:

    http://www.vadriano.com/excel-vb/200...s-in-a-folder/


    Regards, TMS

  9. #9
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to set password based on cell value

    Thanks TMShucks - I'm getting really close but I'm now stuck on the section that starts the CASE select. I am getting a "run time error - 424 Object Required" when it hits this line. The first file in the folder does open and the file name does get populated in cell A1 ( I left that part in to see what gets placed there).

    Please Login or Register  to view this content.

+ 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