Update File path in VBA

BMil8

Board Regular
Joined
Aug 9, 2010
Messages
153
Office Version
  1. 365
Hello,

This is my first post here at MrExcel, but I have been using the forum for some time now to search for answers. I haven't been able to find an answer to my latest question: When I move files/folders referenced in my VBA script, the script obviously fails since the path mentioned is no longer available. Is there a way to have the script update automatically when I change file/folder locations?

For example: Script reads

Workbooks.Open Filename:= _
"\\C:\Documents\Folder1\Folder2\MyFile.xlsx"

If I manually move MyFile from Folder2 to Folder3, I have to manually update the file path in the script. Is there any method to have the path automatically update to something such as

Workbooks.Open Filename:= _
"\\C:\Documents\Folder1\Folder3\MyFile.xlsx"

Thank you,
Brian
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
If the file being opened (MyFile) is in the same folder as the file in which the VBA resides then you can use the path as stipulated by ThisWorkbook, ie:

Code:
Workbooks.Open Replace(ThisWorkbook.FullName,ThisWorkbook.Name,"MyFile.xlsx")

If that is not the case then unless the file name is unique then no, at least NAFAIK... if the file is unique you can search for it but IMO - not ideal.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

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