willow1985
Well-known Member
- Joined
- Jul 24, 2019
- Messages
- 929
- Office Version
- 365
- Platform
- Windows
I was assisted with the below code on this Forum but need further help with it.
The purpose of this code was to disable a excel spreadsheet (close the sheet) if the sheet was copied to another location. This did not have to be fool proof as the users are not well versed in Excel and it is more meant as a deterrent for people making their own copies.
That being said if a copy is made and the directory of the workbook does not contain the file name referenced in Sheets("List").Range("F1") the workbook closes.
What I would like is if it doesnt match, instead of closing the workbook lock it down with a password instead.
Is this possible?
Thank you to anyone who can help.
The purpose of this code was to disable a excel spreadsheet (close the sheet) if the sheet was copied to another location. This did not have to be fool proof as the users are not well versed in Excel and it is more meant as a deterrent for people making their own copies.
That being said if a copy is made and the directory of the workbook does not contain the file name referenced in Sheets("List").Range("F1") the workbook closes.
What I would like is if it doesnt match, instead of closing the workbook lock it down with a password instead.
Is this possible?
Thank you to anyone who can help.
Batch Log.xlsm | ||||
---|---|---|---|---|
E | F | |||
1 | First file in Directory: | AEROSPACE | ||
2 | \\DAVIN\CADO\AEROSPACE\Batch Records\Batch Log.xlsm | |||
List |
VBA Code:
Private Sub Workbook_Open()
Application.Calculation = xlAutomatic
Application.CalculateBeforeSave = True
Dim firstFolder As String
Dim fn1 As String
Dim fn2 As String
Dim FullFileName As String
Const AllowCancel = False
FullFileName = Sheets("List").Range("F2").Value
firstFolder = Sheets("List").Range("F1").Value
With ActiveWorkbook
On Error Resume Next
fn1 = Mid(.FullName, InStr(UCase(.FullName), UCase(firstFolder)))
fn2 = Mid(FullFileName, InStr(UCase(FullFileName), UCase(firstFolder)))
If fn1 <> fn2 Then
Dim choice As Long, bttns As Long
If AllowCancel Then bttns = vbOKCancel Else bttns = vbOKOnly
choice = MsgBox("This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook.", bttns)
If choice = vbOK Then .Close False
End If
End With
End Sub