+ Reply to Thread
Results 1 to 5 of 5

Force a form to close in a new opened worksheet

  1. #1
    Registered User
    Join Date
    04-27-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Force a form to close in a new opened worksheet

    Hi, I hope someone can help.

    I have a workbook (Book1.xls) which, by using a button on a form, opens a second workbook (Book2.xls) and copies data from it into the original (Book1.xls).

    The second workbook automatically opens a form when it is opened, and this is bringing the process to a halt before I get to the bit where I want to close Book2.xls. The code works fine but you have to manually close the form in the newly opened workbook before the process completes. Because the code will ultimately be used for duplicating more than one workbook this is not desirable - I want the process to complete without the user having to manually close down the forms which pop up every time a new file is opened ready for copying.

    Can anyone tell me how to select the open form in Book2.xls and force its closure so the code continues to run without the user having to manually close each form.

    Thanks in advance for any responses

    Here is the code I have tried using which does NOT work:
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 05-02-2012 at 05:44 PM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Force a form to close in a new opened worksheet

    The second workbook automatically opens a form when it is opened, and this is bringing the process to a halt before I get to the bit where I want to close Book2.xls.
    This feels like the form is being called under the Workbook_Open event within the ThisWorkbook sheet object. I would either add conditional language to determine if you would indeed like it opened, or just delete it and manage its call outside of this event.

    Hope this helps,

    -as-
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Force a form to close in a new opened worksheet

    Hello there,

    In the OldWorkBook you could place the following code which determines if the NewWorkBook is open and if it is then don't show form, if it's not the show userform on open.

    Please Login or Register  to view this content.
    Code found at

    http://www.exceltip.com/st/Determine_if_a_workbook_is_already_open_using_VBA_in_Microsoft_Excel/472.html

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Force a form to close in a new opened worksheet

    Hello Islwyn,

    The code halts because the UserForm being shown is Modal. This means that the application program hosting the UserForm will not accept input until the UserForm is closed. You can change this default property setting using the Properties window. Change the ShowModal property to False and save your changes. Next time, the UserForm will be displayed but allow the VBA to continue.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    04-27-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Force a form to close in a new opened worksheet

    Thanks for all your replies. I should have said that I did look at changing the modal option but was keen to avoid this as I want to lock users into using single forms. rvasquez's WorkbookOpen function looks ideal so I will give it a try.

    Cheers

    Is

+ 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