VBA code for a message box on exit depends on a cell value

  • Hi
    I have an excel form for reporting the result of a test. I used a formula to determine if the test is PASS or FAIL.
    I want to use a VBA code to warn the user on exit if the result is Fail and asked to review if necessary.
    I tried some codes(like below) but did not work and also data validation is not working too!
    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ThisWorkbook.Sheets("CSG 130").Range("I$64").Value2 <> "PASS" Then
    MsgBox "Analysis Outcome is Failed!!!!"
    Cancel = True
    End If
    End Sub[/VBA]


    Any suggestion, please?

  • Hi,


    Without our workbook ... it is quite difficult to guess what might be the problem ... :wink:


    Make sure the reference sheet 's name is precisely CSG 130


    Make sure the cell I64 shows either PASS or FAIL


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • See if this helps.


    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If UCase(ThisWorkbook.Sheets("CSG 130").Range("I$64").Value2) <> "PASS" Then
    MsgBox "Analysis Outcome is Failed!!!!"
    Cancel = True
    End If
    End Sub
  • Thanks, Carim
    No, did not work!
    I tried data Validation too and not working either.


    As I said earlier ... do not hesitate to attach your workbook with your next message ... :wink:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • See if this helps.


    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If UCase(ThisWorkbook.Sheets("CSG 130").Range("I$64").Value2) <> "PASS" Then
    MsgBox "Analysis Outcome is Failed!!!!"
    Cancel = True
    End If
    End Sub


    Thank you fro your time.
    Unfortunately, didn't work :?

  • Hello,


    First Question : Is it working or not ...???


    The macro is as follows :


    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If ThisWorkbook.Sheets("CSG 130").Range("I64").Value <> "PASS" Then
        MsgBox "Analysis Outcome is Failed!!!!"
        Cancel = True
      Else
        MsgBox "Well Done !!!!"
      End If
    End Sub


    AND ... more importantly ... it is stored in the Module ... ThisWorbook ...


    HTH

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)


  • Hi Carim
    Thank you, Yes this code worked, but I can't close the workbook now!?
    Have you done any modification? This code is not working on another form as I have 50 forms to be set!

  • Hello,


    No modification ...


    You have designed your macro to force User to go back to the test untill he gets "PASS" ...


    If you only need to have your messages displayed and let the User close the file regardless of his result ...


    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If ThisWorkbook.Sheets("CSG 130").Range("I64").Value <> "PASS" Then
        MsgBox "Analysis Outcome is Failed!!!!"
      Else
        MsgBox "Well Done !!!!"
      End If
    End Sub


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!