我已经做了一个子例程来检查密码和预定义的到期日期。代码工作,但只有在打开Excel工作簿。以下是一些代码,以显示我到目前为止:
Private Sub Workbook_Open()
Dim j, i, trials, passCnt As Integer
trials = 3 'Number of trials
passCnt = 4 'Number of times to enter password
j = 1
ReDim AllPassWords(1 To trials) As String
AllPassWords(1) = "123"
AllPassWords(2) = "456"
AllPassWords(3) = "789"
ReDim ExpDate(1 To trials) As Date 'We pre-define the expiry dates and passwords
ExpDate(1) = CStr(DateSerial(2023, 1, 13) + TimeSerial(8, 49, 0))
ExpDate(2) = CStr(DateSerial(2023, 1, 13) + TimeSerial(8, 51, 0))
ExpDate(3) = CStr(DateSerial(2023, 1, 13) + TimeSerial(8, 53, 0))
Dim PassWord As String 'User password
If CDate(Now) < ExpDate(j) Then 'If the jth trial has not expired we do the following
If j = 1 Then
For i = 1 To passCnt ' chances to enter password
'Enter password before we can use the worksheet
PassWord = InputBox("Please input password.")
If PassWord = AllPassWords(j) Then
Exit For
ElseIf i < passCnt Then
MsgBox "Incorrect password. " & passCnt - i & " attempts remaining."
ElseIf i = passCnt Then
MsgBox "Password limit reached. Closing workbook"
ThisWorkbook.Close
End If
Next i
MsgBox ("You have " & ExpDate(j) - CDate(Now) & " days left")
End If
Else: MsgBox "Trial " & j & " has expired. New password will be required to continue"
j = j + 1
End If
我需要这个子运行,而工作簿是打开的,以便当超过有效期的应用程序将要求一个新的密码。用户可以只是保持工作簿打开,并有试用期无限期。另一个问题,我担心的是,'msgbox'将弹出每次执行代码。有没有办法运行代码,而工作簿是打开的,但同时阻止msgbox显示,除非用户打开工作簿。
2条答案
按热度按时间tjvv9vkg1#
你可以这样做
在本练习册上填写:
然后在单独的模块中创建另外两个宏
我以前
要定义仅当打开工作簿时显示的内容,您可以根据需要进行调整
w46czmvw2#
我认为这可能会起作用。我添加了另一个子例程,用于检查是否有任何工作表被更改或单元格被单击。然后,新的子Workbook_SheetChange检查相同的预定义日期,并调用原始的子Workbook_Open。因此,当工作簿打开时,用户将收到试用期已过期的消息,需要新密码才能开始下一次试用。以下是代码: