VBA to Lock and prevent opening of the excel file

Excelguruintraining

New Member
Joined
Apr 23, 2015
Messages
24
Hello, I hope all is well. I’m trying to write a code that I can add to the macros I have created so that after a certain date the workbook saves and enters a password that will prevent the workbook from opening. I have come up with the code below but can’t seem to get it to work. Any help is appreciated.
Code:
If CDate(Now) > CDate(6 / 8 / 2015) ThenMsgBox "workbook has expired", vbOKOnly
ActiveWorkbook.SaveAs Filename:= _
                    fil.Path, _
                     Password:="Amco32", ReadOnlyRecommended:=False _
                    , CreateBackup:=False
End If
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi try this:

Code:
If Date > CDate("6/8/2015") Then


MsgBox "workbook has expired", vbOKOnly


Else
' valid - still in date


End If

Your sample date is for August so code will pass valid.

Also, be mindful this type of control only works if your users enable macros.

Dave
 
Upvote 0
This only displays a message box, it does not lock the workbook.

What I did was an example for the valid date test part only

- you will need to add your code in appropriate parts as required.

Something like ThisWorkbook.Close False after the message box would close workbook without saving.

Dave
 
Upvote 0
Maybe we can solve each others problem. I have an expired password and am trying to bypass this code....


'Checking expiration
If Sheet3.Cells(69, 2) = 0 Then
MsgBox "Activation password for the spreadsheet has expired,"
Exit Sub
End If
BinOK = False
Me.pbResults.Value = 0
wban = Sheet3.Cells(8, 2)
wban = fWban(wban)
fName = ThisWorkbook.Path & "\WDF\" & wban & ".wdf"
If Dir(fName) = "" Then
MsgBox "Missing file " & wban & ".wdf " & "in the WDF folder. Program is terminated!", , "Error"
Exit Sub
End If

Sheet 3 is not visible and I have tried various methods to see it but have been unsuccessful.

Any help would be appreciated.
 
Upvote 0

Forum statistics

Threads
1,203,524
Messages
6,055,904
Members
444,832
Latest member
bgunnett8

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