+ Reply to Thread
Results 1 to 7 of 7

BeforeClose and BeforeSave interaction

  1. #1
    Registered User
    Join Date
    11-10-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    BeforeClose and BeforeSave interaction

    Hi all,

    I've written a macro for which works fine when the user saves the workbook the macro spits out a look up table into a second workbook. When the user closes the workbook I have another macro which runs and hides all the other sheets apart from the sheet that says Macros need to be enabled for this macro to work. It then does a save. This calls the before save macro into effect and it won't allow the second workbook to be set and therefore it crashes.

    Please Login or Register  to view this content.
    The code always crashes as the ExtRevenueBk will not set and is therefore nothing.

    Anyone got any ideas as I'm currently going bald!!

    Many thanks in advance.
    Last edited by fredblogs; 05-25-2010 at 06:36 AM.

  2. #2
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: BeforeClose and BeforeSave interaction

    Hi fredblogs.
    What error message do you get?
    You have that code on the ' Private Sub Workbook_BeforeClose(Cancel As Boolean)' ?

    Just an info:
    On the following line
    Please Login or Register  to view this content.
    you are defining wkb as variant and only ExtRevenueBk as Workbook. The same happens your next Dim line. The 'Dim' statement is optional after the first one (in a row). But the 'as ....' MUST allways be written for each variable. Else, Excel will define it as type 'Variat'

  3. #3
    Registered User
    Join Date
    11-10-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: BeforeClose and BeforeSave interaction

    Hi Wizz,

    Thank you for the post and I will bear your advice in mind going forward. I've just made the changes you recommended and I'm still getting the same error. I'm getting an Object Variable or With Block Variable not set error message.

  4. #4
    Registered User
    Join Date
    11-10-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: BeforeClose and BeforeSave interaction

    Hi Wizz... again,

    The code is in a sub in a module and I'm calling it from the private before close sub. I have to say I can change it back very quickly it was one of my desperate attempts to try and get it to work.

  5. #5
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: BeforeClose and BeforeSave interaction

    Fredblogs,
    Having the code in a module is O.K. I just wanted to be sure you were calling it from the on workbook close event.

    I tested the code you wrote and it worked fine.
    Have you verified the path and name from file?
    You are using Excel2007 or higher
    Do you have more code lines between the procedure call and the line that pops the error?

  6. #6
    Registered User
    Join Date
    11-10-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: BeforeClose and BeforeSave interaction

    Hi Wizz,

    Thank you so much for you're help so far and apologies for the delayed response I was off yesterday. I'm using Excel 2007. The call is coming from the before save event. This is, I think, where my problem is lying as the line that saves the workbook in the before close event is firing the before save macro and creating the error. I've never been trained in excel so while I try to do things the best way these aren't necessarily the correct way (as you pointed out with the as statements). I've just had a think about it and I think you've cracked it I have changed the call to the before close event and it worked first time. It will work fine from here and I don't need it to happen every time the book is saved. I can do it when the book is closed. Thank you so much for your help. fb

  7. #7
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: BeforeClose and BeforeSave interaction

    It was a pleasure.
    Just keep learning; VBA+Excel is a beautiful and very powerful tool!

    Saludos

+ 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