• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Message Box

CalinDoo

New Member
I am fairly new to Excel, and wish to create some code that can do two things upon exiting file. a) Display an AbortIgnoreCancel message box dependant upon cell a1 contents. Where if cell a1 has no result to continue or if it displays error to display the message warning. Should there be no error or if the message choice is Ignored then b) a further message box to be displayed with a simple reminder with a YesNo choice. Yes continue to exit or No to cancel exit.

Whilst I have had some success with the above as two seperate functions, I have been far from successful in trying toincorporate both within one function.

Hence any help or pointers would be gratefully received.

Many thanks
 
Hi CalinDoo, and welcome to the forum. :awesome:

This should give you some guidance
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim myCell As Range

'Where is the cell of interest?
Set myCell = Worksheets("Sheet1").Range("A1")

'I wasn't able to fully follow you logic, but hopefully you can ammend as needed
If myCell.Value <> "" Then
    'If cell if blank, do nothing
   
    If IsError(myCell.Value) Then
        'If there's an error, then display the message
        If MsgBox("There is an error in A1", vbAbortRetryIgnore, "Error found") <> vbIgnore Then
            Cancel = True
        Else
            'Second warning
            If MsgBox("Last change...sure you want to exit?") <> vbYes Then
                Cancel = True
            End If
        End If
    End If
End If
       
End Sub
 
Back
Top