Workbook_BeforeClose event macro dont work optimal.

abjac

Board Regular
Joined
Feb 18, 2013
Messages
74
HI I have below code which give me little problems. When there is a value in cell D10. a messagebox pop up and ask me to save the invoice , before i can close the workbook. It works ok. If there is no value it close right away. If there is a value it save the worksheet, but dont close the workbook. So basically its works.

I would like that if there is value in cell D10, dont save the worksheet, but dont close the workbook off course..But only save, when i have saved the invoice, as it ask me to do.
Its more that when there will be many invoices, it will take un nesssary time. I am sure it is maybe only a little thing. But i have played around and cant make it work.

And off course the hold idea is that it save without promt the user.

Please have a look at the code.

Sincerely
Abjac

Ps. Have crossposted this in below link, but sofar no answers, which solve my problem.

Minor error gives problem in code in Workbook_BeforeClose event

Code:
<DIR>[SIZE=2]Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Masterinvoice").Activate
If Range("D10").Value > 0 Then
MsgBox ("You Have to save the invoice, before you can close the workbook")
Cancel = True
End If
Call setpass
Call TwoSheetsAndYourOut
Sheets("Masterinvoice").Activate
'Save changes to workbook when closing, without prompting user.
Me.Save
End Sub
</DIR>[/SIZE]
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Jim. Then it will close the workbook and save, which it shall not. If there is a value in D10. It shall not close.
It dont close right now. It come with the message, but it save.
I would like it to only save, when the workbook close. Because if there example is many invoices and date, its unnessasary time with this extra save.
I thought it was easy to make, but i have had 92 watching this thread in the other board, and no answer could solve this.
So any idea would be great

Sincerely

Abjac
 
Upvote 0
Finally after playing around alot i found out of this.
The working code is like this and it only save when there is no value in cell D10
Insert Me.Saved = False did the trick

Thanks to my self haha
Sincerelyu
Abjac
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Masterinvoice").Activate
If Range("D10").Value > 0 Then
MsgBox ("You Have to save the invoice, before you can close the workbook")
Cancel = True
Me.Saved = False
Else
Sheets("Masterinvoice").Activate
'Save changes to workbook when closing, without prompting user.
Me.Save
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,164
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top