+ Reply to Thread
Results 1 to 7 of 7

VBA code to update query from a password protected excel workbook and then close workbook

  1. #1
    Registered User
    Join Date
    06-06-2017
    Location
    Wimborne, England
    MS-Off Ver
    Office 365 Business
    Posts
    30

    Question VBA code to update query from a password protected excel workbook and then close workbook

    I am a new member to the forum. I've looked through the various threads, but can't find a solution to my current problem. I have a query in one workbook that updates from a table in another password protected workbook. A password is required both to open and modify the workbook. I need VBA code to open the password protected workbook, update the query, and then close the workbook, after the query has refreshed.

    I have got as far as opening the workbook and refreshing the query. The two areas I need help with are:

    1. what is the code to get past the modify password?

    2. what is the code to close the workbook only after the query has refreshed? (I can close the workbook, but it does it before the refresh is complete and the refresh therefore fails)

    Obviously I have access to all passwords.

    Code extract

    [I]
    Please Login or Register  to view this content.
    Last edited by MrChipper; 06-09-2017 at 04:39 AM.

  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,425

    Re: VBA code to update query from a password protected excel workbook and then close workb

    Welcome to the forum

    See: https://msdn.microsoft.com/en-us/lib.../ff194819.aspx


    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    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
    Registered User
    Join Date
    06-06-2017
    Location
    Wimborne, England
    MS-Off Ver
    Office 365 Business
    Posts
    30

    Re: VBA code to update query from a password protected excel workbook and then close workb

    Thank you, Trevor, for putting me right on the correct etiquette. Also your link helped me identify the correct syntax for the read/write access. I still need to figure out how to delay closing the opened workbook until after the query refresh has completed.

  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,425

    Re: VBA code to update query from a password protected excel workbook and then close workb

    You're welcome. Thanks for the rep.


    You could maybe put in a loop ... with a very high counter, or perhaps use On Time to delay the close.

  5. #5
    Registered User
    Join Date
    06-06-2017
    Location
    Wimborne, England
    MS-Off Ver
    Office 365 Business
    Posts
    30

    Re: VBA code to update query from a password protected excel workbook and then close workb

    I tried the code below as a timer, but all that happened is that, even though this was added after the refresh instruction, the blue circle spun for 5 minutes after the workbook open, then it closed and then it asked me for the passwords, reopened and then did the refresh after which I had to close the workbook manually.

    Please Login or Register  to view this content.
    I did wonder whether it had something to do with running the query in background, but when I changed this, the refresh didn't complete.

  6. #6
    Registered User
    Join Date
    06-06-2017
    Location
    Wimborne, England
    MS-Off Ver
    Office 365 Business
    Posts
    30

    Re: VBA code to update query from a password protected excel workbook and then close workb

    Quote Originally Posted by MrChipper View Post
    I tried the code below as a timer, but all that happened is that, even though this was added after the refresh instruction, the blue circle spun for 5 minutes after the workbook open, then it closed and then it asked me for the passwords, reopened and then did the refresh after which I had to close the workbook manually.

    Please Login or Register  to view this content.
    I did wonder whether it had something to do with running the query in background, but when I changed this, the refresh didn't complete.

    Ok. The following code worked. I used this Loop to delay the close of the workbook:

    Please Login or Register  to view this content.

  7. #7
    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,425

    Re: VBA code to update query from a password protected excel workbook and then close workb

    Thanks for sharing your solution. I'm sure others wll find it useful.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to run a macro behind a Password Protected excel workbook
    By chrisandsally in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-16-2015, 08:05 AM
  2. [SOLVED] Code to cut cell from one workbook, close same workbook, and paste in different workbook
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-13-2014, 04:01 PM
  3. unable to close password protected workbook using VBA code in another workbook
    By abulooz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-04-2012, 08:54 AM
  4. Create Query from Password Protected Workbook
    By Joy C in forum Excel General
    Replies: 0
    Last Post: 03-25-2011, 05:46 AM
  5. Refresh query with a password protected workbook.
    By Joy C in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2011, 12:31 PM
  6. code to update very hidden sheets in a protected workbook
    By Balachan56 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-22-2009, 05:31 PM
  7. Replies: 2
    Last Post: 03-30-2005, 09:06 AM

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