Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: xxxxxxxWorkbook.Close not working!

  1. #1
    Board Regular
    Join Date
    Apr 2002
    Posts
    53
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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. #2
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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. #3
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.
    Best regards,
    Allan Chara
    http://www.mrspreadsheets.com

  4. #4
    Board Regular
    Join Date
    Feb 2002
    Location
    Stockton, California
    Posts
    281
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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. #5
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Robfo,

    That shouldn't be a problem. Because ThisWorkbook is just referring to the workbook that houses the code.

  6. #6
    Board Regular
    Join Date
    Apr 2002
    Posts
    53
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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


Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •