Password with Time and Date

GreenBolt

New Member
Joined
Aug 2, 2017
Messages
7
Hi all,

I'd appreciate your help with the following please.

Basically I'm trying to protect a workbook after a Date. Then if someone enters the correct password then the workbook becomes unprotected, otherwise if they get the password incorrect, then it protects the workbook till you get it right. The first half of the code appears to be working, however after the "END IF" statements, that's where I go wrong.

Example Code - (almost working)

Sub Workbook_Open()
Dim exdate As Date, ws As Worksheet, PW As String
exdate = "03/08/2017"
If Date > exdate Then
MsgBox ("You have reached the end of your trial period")
PW = InputBox("Enter password:")
If PW = "Password" Then
For Each ws In Worksheets
ws.Unprotect
Next ws
MsgBox ("Password is Correct, Please Proceed")
Exit Sub
End If
End If
For Each ws In Worksheets
ws.Protect
Next ws
MsgBox ("The Password is incorrect, This file is now locked from any further use")

MsgBox ("Contact Support on XXX XXX XXX for the Password")

End Sub



cheers - GreenBolt
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
What do u want to achieve with the macro ? Close the file or Password protect the sheets?

Thank you for your reply.

Basically, I'd like to have all of the worksheets password protected after a certain date. Once you request for a password and I give it to you, then the password unprotects all of the worksheets, and you can start using the worksheets as per normal.


cheers - GreenBolt
 
Upvote 0
Try the below code, I have added 1 more variable MyPassword

Code:
Sub Workbook_Open()

Dim exdate As Date, ws As Worksheet, PW As String, MyPassword As String
exdate = "03/08/2017"

MyPassword = "Password"

If Date > exdate Then
    MsgBox "You have reached the end of your trial period", vbExclamation
    PW = InputBox("Enter password:")
        If PW = MyPassword Then
            For Each ws In Worksheets
               ws.Unprotect Password:=MyPassword
            Next ws
        MsgBox "Password is Correct, Please Proceed", vbInformation
        Else
            For Each ws In Worksheets
                ws.Protect Password:=MyPassword
            Next ws
        MsgBox "The Password is incorrect, This file is now locked from any further use" _
        & vbNewLine & "Contact Support on XXX XXX XXX for the Password", vbCritical
        End If
End If

End Sub
 
Upvote 0
Hi Mse330,


Thank you for the reply.

I've tried your code and it works like a charm.

Thank you once again -

cheers - GreenBolt
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,816
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top