Forum Discussion
VBA Timebomb in Excel Spreadsheet
- Aug 30, 2021
Thanks, I think the problem was that the ExpiredDate name refers to the date as a string.
The attached version refers to the date as a date. See if it works for you.
The code sets up the ExpirationDate name the first time it is run.
If you change the value of C_NUM_DAYS_UNTIL_EXPIRATION after that, it won't affect the value of ExpirationDate. You'd have to remove the defined name and run the code again.
It'd be helpful if you could attach a sample workbook with the complete code.
HansVogelaar Thank You very Much Hans for the Reply, I really Appreciate it. Understood on the First Issue, it makes sense else the Date will keep on changing. Please find Attached a Sample Workbook with the Code. Please let me know if you require anything else.
There are two Modules, modTimeBomb with the Code in and unHideDate to show the Hidden Named Range Created.
Regards
Kennedy
- HansVogelaarAug 30, 2021MVP
Thanks, I think the problem was that the ExpiredDate name refers to the date as a string.
The attached version refers to the date as a date. See if it works for you.
- MCI-ITAug 30, 2021Copper ContributorHans Vogelaar, Absolute Legend!!!!
This Code Seems to Work Better Than My Social Life!
Just a Question or Two:
1. Instead of Showing the Date when I UnHide the Named Range, it shows Numbers, there a Way to Convert that back to Date Show when It will Expire?
2. Instead of Setting it on a Number of Days After First Opening, How can I set a Fixed Date for it to Expire, like 31/12/2021, So it Ignores the 30 Days and Just look for >= 31/12/2021
3. If I protect the VB Project with a Password, will that have any Effect?
Again, Thank You so Much!- HansVogelaarAug 30, 2021MVP
1. Displaying a number is a side effect of using a date instead of a string. It shouldn't really matter.
2. Using a fixed date will greatly simplify the code - no defined name needed:
Sub TimeBombMakeReadOnly() '''''''''''''''''''''''''''''''''''''''''''''''''''''' ' If today is past the expiration date, make the ' workbook read only. '''''''''''''''''''''''''''''''''''''''''''''''''''''' If Date >= DateSerial(2021, 12, 31) Then ThisWorkbook.ChangeFileAccess xlReadOnly ThisWorkbook.Save End If End Sub3. No, that doesn't matter.