Set expiry date 10 days from opening workbook

bptaw

Board Regular
Joined
Feb 27, 2017
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I currently have the following code in a user form:
Private Sub cmdLogin_Click()

Dim user As String
Dim password As String
user = Me.txtUserID.Value
password = Me.txtPassword.Value

If Date > DateValue("14-09-2023") Then
MsgBox "This Trial version has now expired and the Workbook will close."
ActiveWorkbook.Close SaveChanges:=False
Exit Sub

End If
If Date <= DateValue("14-09-2023") Then
MsgBox "This is a trial version and will expire on 14 September 2023."

End If

I want to change it so that the expiry date is 10 days from first opening the workbook. Anyone help?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
you can use "open" event of worbook to save number of times you opened workbook and date of first time opened to a sheet and hide that sheet:
VBA Code:
Private Sub Workbook_Open()
    Sheet1.Cells(1, 1).Value = Sheet1.Cells(1, 1).Value + 1 'count number of times the file is opened
    If Sheet1.Cells(1, 1).Value = 1 Then 'detect first time you open workbook
        Sheet1.Cells(1, 2).Value = Date  'save the date of first time
    End If
End Sub
 
Upvote 0
you can use "open" event of worbook to save number of times you opened workbook and date of first time opened to a sheet and hide that sheet:
VBA Code:
Private Sub Workbook_Open()
    Sheet1.Cells(1, 1).Value = Sheet1.Cells(1, 1).Value + 1 'count number of times the file is opened
    If Sheet1.Cells(1, 1).Value = 1 Then 'detect first time you open workbook
        Sheet1.Cells(1, 2).Value = Date  'save the date of first time
    End If
End Sub
or just save the date of first time you open workbook
VBA Code:
Private Sub Workbook_Open()
    If IsEmpty(Sheet1.Cells(1, 1)) Then
        Sheet1.Cells(1, 1).Value = Date 'save date of first time open
    End If
    'call your macro
End Sub
 
Upvote 0
VBA Code:
Private Sub Workbook_Open()
If [ISREF('Hiddensheet'!A1)] Then
    If Date > Sheets("HiddenSheet").[A1] Then
        MsgBox "This Trial version has now expired and the Workbook will close."
        ActiveWorkbook.Close SaveChanges:=False
    Else: MsgBox "This is a trial version and will expire on " & Sheets("HiddenSheet").[A1]
    End If
Else
    Worksheets.Add.Name = "HiddenSheet"
    Sheets("HiddenSheet").[A1] = Date + 10
    Sheets("HiddenSheet").Visible = False
    MsgBox "This is a trial version and will expire on " & Sheets("HiddenSheet").[A1]
End If
End Sub

NOTE :
Users can work around this by :
• Disabling macros, or
• Disabling events, or
• Deleting the HiddenSheet, or
• Manually changing the date on the HiddenSheet.
 
Upvote 0
Solution
Thanks for all these responses, but I am still new at VBA not quite clear on how to implement any of these options. Can you please help me better understand the options you are suggesting and how they can fit in my login module?
I do understand that Workbooks are ultimately not secure and whatever measures I take will only work with the less adventurous.
 
Upvote 0
Place the code in post #4 in the ThisWorkbook module, save the file, close it, and then open it.
 
Upvote 0
OK, got that working, but I have come across a curious situation. I have been experimenting with changing the number of days but for some reason it does not work. In addition I cannot find the HiddenSheet in the list in the VBE and the expiry date is fixed. Any explanation?
 
Upvote 0
I have since fixed the issue by changing the name of HiddenSheet and it now appears in the VBE. But I am still curious as to how it was previously completely hidden.
 
Upvote 0
OK, got that working, but I have come across a curious situation. I have been experimenting with changing the number of days but for some reason it does not work. In addition I cannot find the HiddenSheet in the list in the VBE and the expiry date is fixed. Any explanation?
Re changing number of days :
• Change the code before pasting to a new workbook, or
• Delete the HiddenSheet, change the days in the code, and re-open, or
• Manually change the date in A1 on HiddenSheet.

Have no explanation as to why HiddenSheet did not appear in the VBE - perhaps you were checking before it was created.
 
Upvote 0
Thanks everyone for all your help. I think I am on top of this now and have also been able to get all bar the entry sheet to be deleted when the trial expires. Unless some hacks the VBA before expiry the sheets are gone anyway (I think).
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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