Results 1 to 2 of 2

Thread: In VBA fro Excel how can I create a AutoExit Code?

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Da_Silvy
    This looks like a useful piece of code. I am wondering if you know what to put in this sub to stop it from closing. I mean sth like the equivalent of "Exit Sub" in VB.

    What I want to do should look like this:

    If Worksheets("Sheet1").Cells("A1").Value <> "" Then
    MsgBox("You have data in cell A1. The program can't close.")
    Exit Sub ' stops the close
    End If

    Is that possible?

    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    try this on for size:

    in the editor, click on ThisWorkbook
    then add this code:
    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ThisWorkbook.Sheets("Sheet1").Range("A1").Value <> "" Then
    MsgBox ("You have data in cell A1. The program can't close.")
    Cancel = True
    End If
    End Sub
    How did that work?

Posting Permissions

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



Click Here to Expand Forum to Full Width