+ Reply to Thread
Results 1 to 10 of 10

vba to close a workbook

  1. #1
    Registered User
    Join Date
    10-22-2014
    Location
    england
    MS-Off Ver
    2007
    Posts
    9

    vba to close a workbook

    Hi,

    I have a macro to copy a range of cells from one workbook to another. It starts by checking if the source workbook is open and either opening or activating accordingly.

    At the end of the Macro I would like it to close the source workbook if it wasn't already open at the start. Is that possible?

    This is the macro:

    Please Login or Register  to view this content.
    Last edited by ALPHAKENNYBODY; 01-22-2015 at 07:31 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: vba to close a workbook

    Hi, Unfortunately 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.

    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



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    10-22-2014
    Location
    england
    MS-Off Ver
    2007
    Posts
    9

    Re: vba to close a workbook

    Quote Originally Posted by Richard Buttrey View Post
    Hi, Unfortunately 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.

    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



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Please accept my humblest of apologies this rule has passed me by. I have wrapped it in code.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: vba to close a workbook

    Perhaps.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    10-22-2014
    Location
    england
    MS-Off Ver
    2007
    Posts
    9

    Re: vba to close a workbook

    That has made it close the source file but it closes it all of the time not just when it wasn't initially open??

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: vba to close a workbook

    This part of the code is wrong.
    Please Login or Register  to view this content.

    You aren't setting Ret to anything.

    Also, you can't activate wkbSource as you haven't set it to refer to anything yet.

    It should probably look more like this.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-22-2014
    Location
    england
    MS-Off Ver
    2007
    Posts
    9

    Re: vba to close a workbook

    Still have the same problem.

    Also I noticed that everywhere that mentions isworkbookopen just puts the file extension in brackets straight after as you have done. For it to work I have to put an = in between isworkbookopen and the brackets. Otherwise I get Compile error: Sub or Function not defined.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: vba to close a workbook

    Do you have a function called IsWorkbookOpen?

  9. #9
    Registered User
    Join Date
    10-22-2014
    Location
    england
    MS-Off Ver
    2007
    Posts
    9

    Re: vba to close a workbook

    Errrrrrrr..... I dunno!

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: vba to close a workbook

    Well it's not in the code you posted and it's not a native Excel VBA function.

    Try searching the workbook for it, and if you don't find it try searching this forum and others for it or any other function that can be used to find out if a workbook is open.

+ 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. Copy from first workbook, close open new workbook paste close
    By johncarr1952 in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 08-04-2021, 08:50 AM
  2. vba code to disable only close button and close through a button in Excel workbook
    By atif_ar in forum Excel Programming / VBA / Macros
    Replies: 47
    Last Post: 11-23-2014, 02:41 PM
  3. [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
  4. Disable close workbook but still force users to close thru command button
    By rathig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 07:29 AM
  5. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM

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