This example will store the number of times that a workbook is opened in
a Hidden Name. After the specified number of times opened the macro will
not run
Code:
--------------------
'---------------------------------------------------------------------------------------
' Module : Module1
' DateTime : 09/05/2007 08:43
' Author : Roy Cox (royUK)
' Website : 'click here for more examples and Excel Consulting' (
http://www.excel-it.com)
' Purpose : Create a check by using a Hidden Name to control how often a macro runs
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Option Explicit
Const MaxUses As Long = 5 '<- change uses
Private Sub workbook_open()
Dim MyUses As Long
On Error Resume Next
MyUses = Evaluate(ThisWorkbook.Names("USED").RefersTo)
If MyUses < 1 Then MyUses = 0 'first used
MyUses = Evaluate(ThisWorkbook.Names("USED").RefersTo) + 1
MsgBox MyUses
ThisWorkbook.Names.Add Name:="USED", RefersTo:="=" & MyUses
End Sub
Sub Test()
'test if OK to run macro
If [USED] > MaxUses Then Exit Sub
MsgBox "Hello World"
End Sub