ActiveWorkbook.Close closes both target book as well as unsaved workbook

PKFillmore

New Member
Joined
Apr 3, 2012
Messages
24
Hi there folks,
its been awhile since I posted anything so if I did anything wrong please let me know.


Ok to give some background... I have a bunch of code located in an addin which opens whenever I open excel. The addin contains a custom ribbon ui with various buttons doing various things...

A problem I am experiencing is when a user opens excel (a blank unsaved workbook ie 'book1') then uses one of the features of the addin. Each feature calls a logbook for stats purposes, When the logbook closes it closes both the logbook as well as the unsaved workbook 'book1' . this causes the next part of the code to fail since book1 is no longer open.

Even if I have more than 1 workbook open, it always closes the unsaved workbook.

Can anyone provide a little insight?
Thanks


Code:
Sub x()
    'code...
     
        'Open Logbook
        Workbooks.Open filename:=LogBook
        
        'more code... (input log data...)
        
        'close logbook
        ActiveWorkbook.Save
        ActiveWorkbook.Close
    
    'more code... (uses cells references which fail since the unsaved workbook closes)


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
it seems to closes the unsaved workbook if I originally did not key any data in it..
If I open the instance and just key abc in cell A1 the workbook doesn't close...

I might be able to fudge something together by having the code key something in a cell temporary but does anyone have a more elegant solution?
 
Upvote 0
I found a makeshift solution without effecting the workbooks contents...
if I change the saved status to false before opening the logbook excel will think there is data in it.

Code:
  ActiveWorkbook.Saved = False
  workbooks.open filename:=LogBook
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top