Announcement

Collapse
No announcement yet.

Workbook_beforeclose event

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Workbook_beforeclose event

    I am having a little trouble with the code below. If cancel is selected it doesn't make the menu sheet visible again. What am I doing wrong?

    Thanks,

    Daniel

    Code:
    Private Sub workbook_beforeclose(cancel As Boolean)
    
    'If cancel button is selected then show the "menu" sheet
    'This bit doesn't do what I expected.
    If cancel = True Then
    Worksheets("menu").Visible = True
    Worksheets("1").Visible = False
    
      End If
    'This bit works
    Worksheets("1").Visible = True
     Worksheets("menu").Visible = False
    End Sub

  • #2
    Re: Workbook_beforeclose event

    DMariotti,


    The "cancel" variable just tells excel whether or not to actually close the workbook. This is there just in case you have some properties that need setting and if during processing you need to acutally prevent the workbook from closing you would set " Cancel = TRUE "

    When the workbook close event fires it is always "FALSE" hence you never get inside your if statment.
    Regards,
    Barry

    My Favorite New Thing:
    Dynamic Named Ranges



    The alternative for
    "Press Any Key To Continue."

    and we all have one we'd like to use it on

    1. Cross Posting Etiquette
    2. Are You Here To Learn: What Have You Tried?
    3. Your Best Resource to begin learning VBA: RECORD A MACRO . . . Then Study It!

    Comment


    • #3
      Re: Workbook_beforeclose event

      Hi Daniel

      You could simply use the code below
      Code:
      Private Sub workbook_beforeclose(cancel As Boolean)
           Worksheets("menu").Visible = cancel
           Worksheets("1").Visible = Not cancel
      End Sub

      Comment


      • #4
        Re: Workbook_beforeclose event

        Thanks Barry and Dave for the help

        Daniel

        Comment

        Working...
        X