Consulting

Results 1 to 5 of 5

Thread: Excel file close

  1. #1
    VBAX Regular
    Joined
    Feb 2007
    Posts
    17
    Location

    Excel file close

    Hi, can someone help please.....
    I have entered the following code but for some reason when the No button is clicked nothing happens and I have to click it a second time to close the file, the Yes button works fine ?
    Am I missing something really obvious ?
    Any help much appreciated.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Ans As String
    Ans = MsgBox("Do you want to save the workbook?", vbYesNo)
    Select Case Ans
    Case vbYes
    ThisWorkbook.Save
    Case vbNo
    ActiveWorkbook.Close False
    End Select
    End Sub

    Regards T

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What do you want to happen, your code says don't close it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2007
    Posts
    17
    Location
    On vbYes save and close and vbNo close the workbook

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just remove

    [vba]
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Ans As String
    Ans = MsgBox("Do you want to save the workbook?", vbYesNo)
    Select Case Ans
    Case vbYes
    ThisWorkbook.Save
    Case vbNo
    ThisWorkbook.Saved=True
    End Select
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Feb 2007
    Posts
    17
    Location
    Many thanks xld works fine.

Posting Permissions

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