Once saw a "excel-home" produced "About Excel suicide function" video tutorial, after careful study, I wrote down their code, share in this.
Implementation ideas:
Use VBA to create a hidden name, record the number of times the current workbook has been opened, and automatically delete the workbook if the number of times exceeds the specified value.
Application Range:
1, for VBA development, send someone to test a function (because the beta version, should not be released to the outside, so add the self-deletion function).
2, for other reasons, you need to limit the number of user use.
Event code (THISWORKBOOK):
Determines whether the read run times are required when Excel opens. (First to determine the user name, to avoid debugging, delete their "Labor results")
Private Sub Workbook_Open () If "Name " then Pager Readopencount Thisworkbook.save endIfEnd Sub
Procedure Code (module):
1. Add a hidden name to record the number of open times.
Sub addhiddennames () ThisWorkbook.Names.Add Name:="opencount", visible:=False, refersto:= "=0"End Sub
2, read the number of times the workbook has been opened, if the number of "suicide", did not reach the "number of" +1. (This code, defined is 3 times)
SubReadopencount ()DimICount as ByteICount= Evaluate (Thisworkbook.names ("Opencount"). RefersTo) ICount= ICount +1 IfICount >3 Then PagerKillthisworkbookElseThisworkbook.names ("Opencount"). RefersTo ="="&ICountEnd IfEnd Sub
3, to achieve "suicide" function, but do not prompt users.
Sub Killthisworkbook () with ThisWorkbook True . Changefileaccess xlreadonly Kill . FullName . Close endwithend Sub
Although the above code executes, can get the expected results, but do not forget, "macro" can be disabled, and even some users do not know how to "Enable Macros", which is not in the scope of this article.
Using VBA to implement Excel suicidal features