EXCEL VBA Code to Expire the worksheet and close and delete the file.

jamilm

Well-known Member
Joined
Jul 21, 2011
Messages
740
Excel Gurus,

can anyone help with a simple code to expire a workbook within 2 days

can you please put the code where the i have put querstions makrs in the VBA code below? thanks.


Private Sub
Workbook_Open()

If ???????????????????????????????????
Then Application.Run "KillWs"

End Sub

Sub KillWs()

MsgBox "This Workbook is expired", vbExclamation + vbMsgBoxRight

Application.Quit

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Maybe try
Code:
Private Sub Workbook_Open()
Dim Edate As Date
Edate = Format("31/12/2012", "DD/MM/YYYY") ' Replace this with the date you want
If Date > Edate + 2 Then
MsgBox "This workbook is Expired and will now close !!!"
ActiveWorkbook.Close
End Sub
 
Upvote 0
thank you Michael. your code was very useful.

closing for If was missing and i added it and it worked perfectly. thanks.

Private Sub Workbook_Open()
Dim Edate As Date
Edate = Format("31/12/2012", "DD/MM/YYYY") ' Replace this with the date you want
If Date > Edate + 2 Then
MsgBox "This workbook is Expired and will now close !!!"
ActiveWorkbook.Close
End If
End Sub




Maybe try
Code:
Private Sub Workbook_Open()
Dim Edate As Date
Edate = Format("31/12/2012", "DD/MM/YYYY") ' Replace this with the date you want
If Date > Edate + 2 Then
MsgBox "This workbook is Expired and will now close !!!"
ActiveWorkbook.Close
End Sub
 
Upvote 0
Ok, sorry about the missing "END IF"
I didn't have Excel at the time...cheers :beerchug:
 
Upvote 0
Michael,

would there be a possibility for such macro to be time specific? for example, i want my file than only could be opened from 8:00AM to 4:00PM Monday through Friday.

can you help?



thank you Michael. your code was very useful.

closing for If was missing and i added it and it worked perfectly. thanks.

Private Sub Workbook_Open()
Dim Edate As Date
Edate = Format("31/12/2012", "DD/MM/YYYY") ' Replace this with the date you want
If Date > Edate + 2 Then
MsgBox "This workbook is Expired and will now close !!!"
ActiveWorkbook.Close
End If
End Sub
 
Upvote 0
Maybe try
Code:
Private Sub Workbook_Open()
Dim Edate As Date
Edate = Format("31/12/2012", "DD/MM/YYYY") ' Replace this with the date you want
If Date > Edate + 2 Then
MsgBox "This workbook is Expired and will now close !!!"
ActiveWorkbook.Close
End Sub
What if I manually change date of my system ?
 
Upvote 1

Forum statistics

Threads
1,223,715
Messages
6,174,064
Members
452,542
Latest member
Bricklin

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