Compare Dates & Password Protect Workbook Using VBA

Home More Videos

Glenn wants that the Excel worksheets should only be accessible to specific people after expiry of a certain date. He wants to protect such expired worksheets using a password. Such a security method  is often used in business to avoid changes in completely entered and validated data.
All our VBA code is done at the point of opening the relevant workbook because we do not wish to allow everybody to view or work with the data till he has a password. Also we need to compare today's date with the set expiry date.
If the current date is greater than the expiry date the user must enter enter a password to access the worksheets in the workbook. We also check whether the entered password is the one that has been assigned in the code.

P. S.: Let's pray that Sandy doesn't create more havoc.

The complete code is given below:

Private Sub Workbook_Open()
Dim d1 As Date
Dim d2 As Date
Dim password As String
d1 = 1 - 9 - 2012
d2 = Date
If d2 > d1 Then
password = InputBox("enter password")
MsgBox ("Opening file")
End If
If password = "abC123" Then
MsgBox ("Welcome!")
MsgBox ("Incorrect password!")
password = InputBox("enter password again")
End If
End Sub

Watch the video on youtube