ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Hold closing the sheet... (https://www.excelbanter.com/excel-programming/319739-hold-closing-sheet.html)

Sridhar Pentlavalli via OfficeKB.com

Hold closing the sheet...
 

Hi

I have to validate the data at the time of closing the sheet. If some errors are present in the data, I need to throw a message and I should not close the sheet.

I am using the following code to achieve it. Here the problem is I can throw an error message if the data is wrong but how to stop the work book from closing. I have to give a chanse to user to correct the data. How this can be achieved?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

{ Validate the data }

If Error Msgbox ( " Error .... Correct the Data " )

End Sub

--
Message posted via http://www.officekb.com

Nick Hodge

Hold closing the sheet...
 
Sridhar

Set the Cancel Boolean to True and the workbook will not close

Private Sub Workbook_BeforeClose(Cancel As Boolean)

{ Validate the data }

If Error Msgbox ( " Error .... Correct the Data " )
Cancel = True

End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


"Sridhar Pentlavalli via OfficeKB.com" wrote in message
...

Hi

I have to validate the data at the time of closing the sheet. If some
errors are present in the data, I need to throw a message and I should not
close the sheet.

I am using the following code to achieve it. Here the problem is I can
throw an error message if the data is wrong but how to stop the work book
from closing. I have to give a chanse to user to correct the data. How
this can be achieved?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

{ Validate the data }

If Error Msgbox ( " Error .... Correct the Data " )

End Sub

--
Message posted via
http://www.officekb.com



Jason Morin

Hold closing the sheet...
 
I would use the BeforeSave and BeforeClose events in
conjunction with each other. Here's an example:

'//Constructive criticism from VBA programmers appreciated

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As _
Boolean, Cancel As Boolean)
Dim checkRng As Range
Set checkRng = Sheets("Sheet1").Range("A1")
If IsError(checkRng) Then
Cancel = True
MsgBox "Error found in " & _
checkRng.Address(False, False) & "."
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Me.Saved = False Then
MsgBox "Please save before closing."
Cancel = True
End If
End Sub


--
Both macros should be placed in the ThisWorbook module.

HTH
Jason
Atlanta, GA

-----Original Message-----

Hi

I have to validate the data at the time of closing the

sheet. If some errors are present in the data, I need to
throw a message and I should not close the sheet.

I am using the following code to achieve it. Here the

problem is I can throw an error message if the data is
wrong but how to stop the work book from closing. I have
to give a chanse to user to correct the data. How this
can be achieved?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

{ Validate the data }

If Error Msgbox ( " Error .... Correct the Data " )

End Sub

--
Message posted via http://www.officekb.com
.



All times are GMT +1. The time now is 03:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com