Compare Dates Password Protect Workbook Using VBA


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”)
Else
MsgBox (“Opening file”)
End If
If password = “abC123” Then
MsgBox (“Welcome!”)
Else
MsgBox (“Incorrect password!”)
password = InputBox(“enter password again”)
End If
End Sub


Watch the video on YouTube

6 thoughts on “Compare Dates Password Protect Workbook Using VBA

  1. Shimon

    Hi

    I have run this code for “Compare Dates Password Protect Workbook Using VBA”
    and got “syntax Error”

    Please guide me as I do like this functionality.

    Reply
      1. Shimon

        The below code is the same as you have in this very web page right above.
        I can send you the file so you can see for yourself. Please share an email address and I will send it.

        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”)
        Else
        MsgBox (“Opening file”)
        End If
        If password = “abC123_ Then
        MsgBox (“Welcome!”)
        Else
        MsgBox (“Incorrect password!”)
        password = InputBox(“enter password again”)
        End If
        End Sub

        Reply
  2. Pingback: Best Training on Advanced Excel and VBA in Bangalore | jaseemblog

  3. Dhaval Thummar

    I have a big data so I want to add search button in sheet which can find the input which we enter into search box from the one sheet or greater then one sheet.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *