VBA Validate file path

adam2010

New Member
Joined
Apr 19, 2010
Messages
4
I have created a spreadsheet at work I want it to remain in its original loaction and if any one copies it in to another folder I need a code so if its
is copied or moved the vba code prompts the user to only open the file from the original lacation and ends the application.

So realy a code to check Validate file path, where the location of the file is and if it dosn't match the correct original location then it closes.

hope some on can help

thanks
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this in the ThisWorkbook module (in the VBE double click ThisWorkbook and paste in the code). Change the path to suit.

Code:
Private Sub Workbook_Open()
If ThisWorkbook.Path <> "C:\Test" Then
    MsgBox "This workbook must be kept in C:\Test", vbExclamation
    ThisWorkbook.Close savechanges:=False
End If
End Sub
 

adam2010

New Member
Joined
Apr 19, 2010
Messages
4
thats nearly it.

Say the original file is C:\calculator\clculator.exl

and some one has copied the file an moved it to another location
it would not open because the original file location is C:\calculator\clculator.exl

that code worked but closed the spreadsheet in the original location


thanks alot for you help

adam
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Hmm. That didn't happen for me. I saved the file in c:\test, closed it and reopened it and it opened fine. I then moved it to the Desktop, tried opening and got the message and the file closed.
 

adam2010

New Member
Joined
Apr 19, 2010
Messages
4
Iv got it working great exactly what I wanted..... thank you
this is going to help me solve a lot of problems

regards

Adam
 

Watch MrExcel Video

Forum statistics

Threads
1,109,341
Messages
5,528,146
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top