VBA Password protect a Workbook after a certain date

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
I have been looking online for help with this code but haven't quite found what I am looking for and was hoping someone on here could help.

I am looking to lock a workbook with a password after a certain date that will close entirely if the password is incorrect.

Something to this effect:

VBA Code:
Private Sub Workbook_Open()
  Dim exp_date As Date
  exp_date = "9/31/2021"
  If Date > exp_date Then
'Make it prompt for a password, if incorrect 
MsgBox ("Error")
    ActiveWorkbook.Close
'if correct open workbook
End Sub

Any help finishing this code would be greatly appreciated!!

Thank you
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You might consider the following...

VBA Code:
Private Sub Workbook_Open()
Dim exp_date As Date
Dim pword1 As String, pword2 As String

exp_date = "8/31/2021"
pword1 = "1234"

If Date > exp_date Then
    pword2 = Application.InputBox("Please enter your password.", "Login")
    If pword2 = pword1 Then
        GoTo Proceed
    Else
        ThisWorkbook.Close savechanges:=False
    End If
End If
Proceed:
End Sub

Please be aware this is anything but foolproof. For example, simply disabling macros upon start up will circumvent this "security."

Cheers,

Tony
 
Upvote 0
Solution
You might consider the following...

VBA Code:
Private Sub Workbook_Open()
Dim exp_date As Date
Dim pword1 As String, pword2 As String

exp_date = "8/31/2021"
pword1 = "1234"

If Date > exp_date Then
    pword2 = Application.InputBox("Please enter your password.", "Login")
    If pword2 = pword1 Then
        GoTo Proceed
    Else
        ThisWorkbook.Close savechanges:=False
    End If
End If
Proceed:
End Sub

Please be aware this is anything but foolproof. For example, simply disabling macros upon start up will circumvent this "security."

Cheers,

Tony

The guys I work for are not tech savy so this should be sufficient. Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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