Consulting

Results 1 to 11 of 11

Thread: Event Workbook_BeforeClose

  1. #1

    Event Workbook_BeforeClose

    I am trying to update a range for thirty worksheets using another workbook, the issue I have is before closing normally I will have a question asking users whether to do other stuff to the same worksheet.
    Updating is great and the only thing is I have manually answer this question before going to the next workbook.
    The question is asked under the event "Workbook_BeforeClose"
    I have used
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    and I still to have respond to this question.
    I have tried SendKeys, which reacts after the question. Or is there a way to know if the calling comes from another workbook to capture the action to identify this case.
    Any suggestions?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have never been asked to do other stuff. Do you mean it asks if you want to save. If so, set Cancel to True.
    ____________________________________________
    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
    Thanks to the rescue again.
    I would like to save the worksheet since there is update, BUT not to the question just like it does not exist (normally the user will answer by themselves).
    Again the Save is YES.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just save it in that event

    [VBA]Private Sub Workbook_BeforeClose(cancel As Boolean)

    Me.Save
    cancel = True

    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
    Gracias.
    My original event is as followed (courtesy question is there as per company's request)
    Private Sub Workbook_BeforeClose(cancel As Boolean)            Dim result as Integer
    
        Result = MsgBox("Do you want to do something else?" , vbYesNo, "Title1")
        If Result = 6 Then                      'Yes - 6 No - 7
            Form1.show
        End If
    
     End Sub

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use the built-in constants

    [VBA]Private Sub Workbook_BeforeClose(cancel As Boolean)
    Dim result As VbMsgBoxResult

    result = MsgBox("Do you want to do something else?", vbYesNo, "Title1")
    If result = vbYes Then
    Form1.Show
    End If
    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

  7. #7
    Gracias.
    Great tips for me.
    Where do you think I should put in order to "remotely" answer this question
    when I open it from another workbook for updating the worksheet?
    Should I do something inside the Event?
    Thank you!

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Can you explain what you mean by "remotely"? There is nothing remote about this code you have. It will fire when the command is given for that specific workbook to close, given that the EnableEvents property is True (it is by default unless otherwise set). If you mean you want to call this event from somewhere else, you will get this specific code to fire if, from the other workbook, you close it. Does that make sense?

  9. #9
    Thank for the response.
    I have a group of workbooks that I have to update a certain range, instead of updating them one by one I use another workbook with macros to "remotely" have those ranges modified.
    The modification of those ranges run well and the only thing I have issue is there is a question asked every time before closing that those of workbooks.
    And the question is placed on the event "Workbook_BeforeClose".
    How can I ignore this question since I am not a normal user which should be asked before leaving the workbook?
    Thank you.

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    If you don't want the events to fire, set the EnableEvents property to false.

    [vba]Application.enableEvents = False

    '... your code here, closing workbooks, whatever

    Application.Enableevents = true[/vba]

    HTH

  11. #11
    Thanks.

Posting Permissions

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