Closing a workbook if it is not located in a specific directory

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
893
Office Version
  1. 365
Platform
  1. Windows
Hello, I know that it is not possible to prevent someone from copying an excel workbook however I was wondering if it was possible to disable the copied workbook?
That if the sheet is opened from a different location than the original (I can specify the directory in a cell on a hidden sheet) it will close down with a message like "This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook" and close the file.

Is this possible? I have a spreadsheet at work that many users utilize but apparently at one point an individual copied the sheet and now we have overlapping records. It is a mess and I am trying to prevent reoccurance.
Thank you to anyone who can help!
 
The "fn1" calculation must be AFTER your "With Activeworkbook" line.

Note that any references that start with a ".", like .FullName are only valid with in a "With" block.
I am sorry I am still very green with writing VBA. I tried moving the With ActiveWorkbook in multiple areas but still get errors

Now I get Invalid procedure call or argument...

VBA Code:
Private Sub Workbook_Open()
    Application.Calculation = xlAutomatic
    Application.CalculateBeforeSave = True
    Dim firstFolder As String
    Dim fn1 As String
    Dim fn2 As String
    Const AllowCancel = True
   firstFolder = Range("F1")

   With ActiveWorkbook

   fn1 = Mid(.FullName, InStr(UCase(.FullName), UCase(firstFolder))) 'this line has the error according to the debugger
    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

        End If
    End With
End Sub
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Somewhere along the way you dropped your "FullFileName" variable!
You still need this in there, and need to set it equal to something.
All the variables you formula calls need to be defined and set to something.
 
Upvote 0
I added it back in but still get the same error on the same line...

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 = True
FullFileName = Sheets("List").Range("F2").Value
   firstFolder = Range("F1")
   With ActiveWorkbook
   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

        End If
    End With
End Sub

Batch Log.xlsm
EF
1File Directory Main:ADMINISTRATION
2X:\ADMINISTRATION\QAEng\Carla's Documents\Test\Batch Log.xlsm
List
 
Upvote 0
I copied your code "as-is", and debugged it, and do not get any "Invalid procedure call or argument" errors.
 
Upvote 0
I copied your code "as-is", and debugged it, and do not get any "Invalid procedure call or argument" errors.
This is what I am getting:

1646238898985.png
1646238924393.png
 
Upvote 0
If you insert this line of code above the one returning errors, run the code and let me know what it returns:
VBA Code:
MsgBox .FullName & vbCrLf & firstFolder
 
Upvote 0
If you insert this line of code above the one returning errors, run the code and let me know what it returns:
VBA Code:
MsgBox .FullName & vbCrLf & firstFolder

Got it, I didnt specify the sheet called List!
 
Last edited:
Upvote 0
If you insert this line of code above the one returning errors, run the code and let me know what it returns:
VBA Code:
MsgBox .FullName & vbCrLf & firstFolder
Ok it now works in the proper directory however in the wrong directory I get a runtime error instead of the message box I want
 
Upvote 0
If you insert this line of code above the one returning errors, run the code and let me know what it returns:
VBA Code:
MsgBox .FullName & vbCrLf & firstFolder
I moved it to a wrong directory and get the same runtime error with the same highlighted line instead of message box:

"This is a copy of the original workbook and therefore cannot be used. Please open the correct workbook"
and closing the workbook

1646241350103.png
 
Upvote 0
If you insert this line of code above the one returning errors, run the code and let me know what it returns:
VBA Code:
MsgBox .FullName & vbCrLf & firstFolder
I put in an On Error Resume Next statement and that seems to work as I assume that I am only getting the runtime error because they don't match?

What do you think?
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 = True
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

        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,356
Members
449,444
Latest member
abitrandom82

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