Force a Password Change every XX days

ChrisRidgway

New Member
Joined
Jan 25, 2017
Messages
1
I've been using MrExcel for years but always found what I needed and never had a need to post before.

I was hoping someone could help me with a strange request:

Does anyone know of a method of have the code that I could use to force a user to change the workbook Password every 90 days? and possibly have a "your password will expire in X Days" message box simular to how active directory forces the change?

Any replies or direction will be greatly appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
First you're going to need a way of storing the last change date value. Putting it in a worksheet cell is the easiest answer. Anything else will take a bunch more code, time, thinking. Hide the "Settings" sheet if you don't want the user to see it.

Then you need to capture the user with something like "Workbook_BeforeSave"

I prefer "on save" over "on open" because you don't want to bother the user until it's time to set the password, which is at the save event.

NOTE: This code must go inside "ThisWorkbook" object in the Project tree. It won't run from anywhere else.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ExpireDays As Long
    Dim WarnDays As Long
    Dim DaysSinceLast As Long
    Dim LastChange As Date
    
    ExpireDays = 90
    WarnDays = 7
    LastChange = ThisWorkbook.Sheets("Settings").Range("A2").Value
    DaysSinceLast = DateDiff("d", Date, LastChange)
    
    If DaysSinceLast > ExpireDays Then
        'Force user to change password code
        ThisWorkbook.Sheets("Settings").Range("A2").Value = Date
    ElseIf DaysSinceLast > (ExpireDays - WarnDays) Then
        Dim x As Long
        x = ExpireDays - DaysSinceLast
        MsgBox "your password will expire in " & x & " Days"
    Else
        'Normal Save occurs here
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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