xxxxxxxWorkbook.Close not working!

Michael Pettinicchi

Board Regular
Joined
Apr 11, 2002
Messages
53
I have the following code in the "Workbook" of "ThisWorkbook". It works if I open the file directly, i.e., it does the job and closes. But, if I open the file via another Excel file, it does the job but doesn't close. What's wrong?

Private Sub Workbook_Open()
Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
Sheets("Sheet1").Activate
ThisWorkbook.Close (previously "ActiveWorkbook" but changed to "ThisWorkbook" - still doesn't work.)
End Sub

Thanks
Michael
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How are you opening this workbook from another workbook? Depending on what VBA code you're using you may not be triggering the the "Open" event.
 
Upvote 0
Michael,

I hate to say it, but I couldn't get it not to work. I tried opening it by double clicking the file, going to file--open from another excel file, and opening from a macro from another file, and they all opened the file and closed it. Try and give more details.
 
Upvote 0
Im not positive, but im guessing it has something to do with the "thisworkbook.close"...since you have 2 workbooks open, i dont know how it deals with that. try this:

Private Sub Workbook_Open()
currentbook=thisworkbook.name
Application.EnableCancelKey = xlDisabled
Range("a5").Select
Application.CommandBars("Worksheet Menu Bar").Enabled = True
Sheets("Sheet1").Activate
Workbooks(currentbook).Close
End Sub
 
Upvote 0
Robfo,

That shouldn't be a problem. Because ThisWorkbook is just referring to the workbook that houses the code.
 
Upvote 0
I replied to this yesterday but it seems my reply got lost.

I did the "ThisWorkbook" bit but it made no difference.

The code I'm using to open this workbook is:
Private Sub CommandButton1_Click()
OpenFileRtn:
reqfile = FileBox.Value & ".XLS"
On Error GoTo FileErr
Workbooks.Open FileName:=reqfile
GoTo OpenFileEOJ
FileErr:
MsgBox "Unable to open file:
GoTo OpenFileEOJ
OpenFileEOJ:
End Sub

Thanks for you interest & help.
Michael
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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