Validation data on exit Excel or save

  • Thread starter Thread starter Atram Informatika
  • Start date Start date
A

Atram Informatika

How can I validate data when i exit excel aplication or when i save
workbook?
If data not valid then i must display message and do not exit aplication, or
save document.

Thank's
 
The following workbook events should work


Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Validation check code here
'----
'----
'if data not valid then
'Cancel = True
'End If
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
'Validation check code here
'----
'----
'if data not valid then
'Cancel = True
'End If
End Sub
 
Thank's on your answer, for example how would code look like if the cell A1
can not be empty, how can i validate this befor exit or save. Thank's sorry
on my english.
 
Something along these lines


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim DataNotValid As Boolean

If IsEmpty(Range("A1")) Then DataNotValid = True

If DataNotValid Then
Cancel = True
End If
End Sub
 
in the ThisWorkbook module, there are event handlers for Before Save and
Before close. Both of these have arguments which if you set them to false
will cancel the save or the close. Be very careful with this kind of code
however because users get very frustrated when they can not save their work
or exit the program. If it is 5 o'clock and time to go home but they are only
half way done then what are they supposed to do. You are better off to use a
message box to let them know that the spreadsheet is not complete and then
prompt them if they wish to continue saving or exiting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top