+ Reply to Thread
Results 1 to 30 of 30

Closing Open Workbook Without Saving

  1. #1
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Closing Open Workbook Without Saving

    With lots of historic help from members of this site, I have created the following code to copy/paste a range of data from one workbook to another.
    Please Login or Register  to view this content.
    It seems to be working except for two problems;

    1. Although I have said SaveChanges:=False, it seems to be trying to save the origin workbook and I get a message to say that it can't be saved due to being read only.

    2. I am not experienced enough to be able to vary the number of rows based upon the actual count of rows which contain something in column B.

    Would someone be able to help me please? Problem no.1 is the most important.

    Thank you,

    Jess

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    I assume you want the last row in column B on the Origin book?
    Please Login or Register  to view this content.
    Then to copy only to last row:

    Please Login or Register  to view this content.
    As for the wb1 book still trying to save, I see no changes being made to it, so I'm not sure why it would say anything at all, but your syntax is correct. I even tried duplicating your code (different book), made changes to wb1 after it opened, and still got no error...

    Is it possible that wb1 has code that runs on open?

    Sorry, I know 1 was more important than 2, but so far I have no idea what is wrong. If you can upload the workbooks maybe we can dig deeper. If you do upload, remember to remove any sensitive data.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Hi, thank you very much for your reply. The code for the second problem works just brilliantly.

    Yes, wb1 has a macro which runs on opening - is this the problem and is there a solution to it without removing the other macro?

    Kind regards

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    Does the other macro have a variable called wb1?
    If so change the name of the variable in one of the codes.

    Also do you need the other macro to run when opening it with this code?

  5. #5
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    The other macro doesn't have a variable called wb1.
    No, it is not necessary to run the other macro when opening it with this code.

    Thank you for your quick reply.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    In that case try
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Thank you for your suggested changes. The macro progresses to the point of correctly pasting the data but stills fails in the closing steps. I do, however, notice a difference in that the other macro is not running automatically now but the workbook remains open. Just in case, I have misunderstood, here is the current code;

    Please Login or Register  to view this content.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    It still doesn't make sense to me why that would be happening, but you could see if this added line fixes the problem:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    Not sure why it still wants to save Wb1, but try
    Please Login or Register  to view this content.
    Beaten 2it

  10. #10
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    It works perfectly except it won't close the origin workbook. It is not trying to save it now so at least that is ok.

    Here's the code;

    Please Login or Register  to view this content.

  11. #11
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    If you add this msgbox, what does it say?
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Adding that line makes no difference, it fails with a code 400 and I don't get a message.

  13. #13
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    Did it fail with that code before adding the msgbox?

  14. #14
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Yes, it has always failed with code 400 at the last stage

  15. #15
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    is there a workbook close event on the origin workbook? What line is highlighted when you get the 400 error?

  16. #16
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    It would have helped if you had mentioned that earlier

    Try stepping through the code using F8, what happens?

  17. #17
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    hi, thank you for sending additional questions to help me. i have now left work for the day and will have to resume tomorrow as i have no access to company systems outside work hours. again, thank you for helping and i hope i can find someone as helpful tomorrow. best wishes, jess

  18. #18
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Stepping through it and using test messages at various stages, the problem is with the range select line;

    Please Login or Register  to view this content.
    If I remove this line all is good, runs perfectly without errors.

    Placement of the cursor isn't essential and therefore I can keep it deleted but if anyone can see the problem it would help avoid it in the future.

    Thank you once again, Jess

  19. #19
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    Is the active sheet protected?
    What error message did you get?

  20. #20
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Hi, the active worksheet is not protected and the error message with the line included is 400

  21. #21
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    Have you tried qualifying that range by specifying workbook.worksheet.range?

  22. #22
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    No, how would I do that?

    Is it;

    Please Login or Register  to view this content.

  23. #23
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    Yep that is exactly what I meant. In theory if it is already the active book and sheet then you don't need that, but thought it is worth trying.

  24. #24
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    This gives error 400 again. The code does everything until it hits this line

  25. #25
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    When the line executes, is wb2 active and KPI the active sheet?

  26. #26
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Closing Open Workbook Without Saving

    Another thought is using .Activate instead of .Select

  27. #27
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Yes, wb2 is active and KPI is the active sheet.

    Tried .Select instead but same error 400.

    It's a little frustrating but I can just delete the line and it works fine.

  28. #28
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    226

    Re: Closing Open Workbook Without Saving

    Sorry, I meant - tried .Activate but get the same error message

  29. #29
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Closing Open Workbook Without Saving

    If you add this msgbox, what does it say
    Please Login or Register  to view this content.

  30. #30
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Closing Open Workbook Without Saving

    Based on the original code, when the problematic line is executed, wb1 *should* be the active workbook because it never lost focus after it was opened. That line, therefore, has no impact on the macro and can safely be removed I think.

    WBD
    Office 365 on Windows 11, looking for rep!

+ 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. Creating new workbook, saving as csv and then closing - but closing always fails.
    By ella626 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-28-2014, 10:05 PM
  2. Allow saving upon closing a workbook even when saving is disabled.
    By Tino XXL in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2011, 09:09 AM
  3. Replies: 3
    Last Post: 01-06-2009, 01:09 PM
  4. Closing the workbook without saving
    By tushar@excel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2008, 05:20 AM
  5. Closing Workbook without saving Changes
    By Sam I Am in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2008, 08:02 PM
  6. closing a workbook without saving and closing a workbook with saving enabled
    By stanigator in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2007, 02:15 PM
  7. Saving and Closing Workbook
    By Jim28 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2006, 12:30 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