![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Apr 2002
Posts: 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 |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
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.
|
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
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.
__________________
Kind regards, Al Chara |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
|
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 |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
Robfo,
That shouldn't be a problem. Because ThisWorkbook is just referring to the workbook that houses the code. |
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Apr 2002
Posts: 53
|
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 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|