+ Reply to Thread
Results 1 to 20 of 20

Close workbook whose name is different each day.

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Question Close workbook whose name is different each day.

    I have a macro that runs daily. It formats and then copies data from one workbook to another. The first workbook, call it A, needs to be closed after I'm done with it, but it's name is different every day. workbook B stays the same except that I'm adding the data from A to it. I've got everything else working just fine except closing A. I try closing A before switching to B, but then it doesn't copy the data. I tried going back to B and using just the first part of its name (since the first part stays the same then it has the date after) and using * to mark for the rest. This did not work either.

    Anyone have any suggestions?

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

    Re: Close workbook whose name is different each day.

    How are you opening the workbook you want to close?

    If you are using code then create a reference to it when you open it.

    Then you can use that reference in the rest of the code and when you are finished with it to close it.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Close workbook whose name is different each day.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    @Nori - The workbook I'm trying to close is a report that I open from my email.
    @Leo - That won't work as I have several other workbooks open in the same instance that I don't want closed.

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

    Re: Close workbook whose name is different each day.

    Why not open the 2 workbooks A and B in the same instance of Excel, separate from the instance with multiple workbooks open?

  6. #6
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Is there no way to do with the others in the same instance. I don't know that the other users will always do that. And I always have them open, except for A which is new every day.

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

    Re: Close workbook whose name is different each day.

    Is there any way to identify the workbook?

    For example, does it's name always contain a certain word/words?

  8. #8
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Yes, it always starts with Storage_Trending_

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

    Re: Close workbook whose name is different each day.

    You could use this to close the workbook.
    Please Login or Register  to view this content.
    By the way, how are you referring to this workbook in the code that transfers data from it to the other workbook?

  10. #10
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    I don't refer to it. I open it from my email, which opens it in the same instance as the others. Then I am able to run the macros that I have on the other workbooks that are open. I run a setup macro that formats the data then changes the focus to the appropriate workbook to copy it to. I just wanted to close the first workbook as it is not needed once the data is copied over. I'll try your code out to see what happens.

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

    Re: Close workbook whose name is different each day.

    You can't copy/format a workbook without referring to it in some way.

    Does your code assume that the active workbook is the correct workbook?

  12. #12
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    This is the macro that I use. I start the aSetup and it does the rest. I just added your code to it. It did not however close the workbook.

    Please Login or Register  to view this content.

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

    Re: Close workbook whose name is different each day.

    The code I posted will close the first workbook it comes across with a name beginning 'Storage_Trending'.

    Are you sure the name begins with that? No spaces, weird characters, misspelling...

    PS In your code you are referring to a workbook called 'Storage Trending', is the workbook you are copying to?

  14. #14
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Yeah, the name always starts with that, it's the name of the report. Then it has the days date after. There are no spaces the report uses '_' instead of spaces. I ran your code and added breakpoints to watch what it does and it never goes into the 'IF' statement as though none of the workbooks have a name starting with Storage_Trending_

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

    Re: Close workbook whose name is different each day.

    Add this to the code just before the If
    Please Login or Register  to view this content.
    Then run the code, open the immediate window (CTRL+G) and you should see a list of all the open workbooks.

  16. #16
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    The name is in all caps for the workbook that I'm trying to close does that matter? It does show up in the window as the last workbook that is looked at.

  17. #17
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    I did that and I see it run through all the open workbooks that I have. I saw that the workbook I'm trying to close is in all caps so I changed the code to reflect that and now it tries to close the workbook, but wants to save it first. How do I prevent it from trying to save?

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

    Re: Close workbook whose name is different each day.

    Yes it matters, a lot.

    Try this, it converts both the worksheet name and what we are looking for to upper case, so case shouldn't be an issue.
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    05-23-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Close workbook whose name is different each day.

    Awesome, that works! Thanks for all the help!

  20. #20
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Close workbook whose name is different each day.

    if you are opening it from an email attachment it won't have a save location right? so how about:
    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)

Similar Threads

  1. [SOLVED] Closing a Workbook from another workbook: Workbooks.close error: Subscript out of range
    By Coreyusa in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-14-2013, 06:45 PM
  2. 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
  3. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  4. [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
  5. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM

Tags for this Thread

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