Inderect addressing for a file

jborges

New Member
Joined
Apr 22, 2002
Messages
13
How is the way to express the following statment without be specific with the name of the directory, I mean that I would like to open a file in the same directory where the excel file is but without specifying the name of the directory itself. The idea is to have a portable application not depending on the name of the directory which contains the files:
I have:
Workbooks.Open "I:directory cost.xls"

But I would like to do something like:

Workbooks.Open "\cost.xls"

but doesn´t work

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
L

Legacy 7259

Guest
Try this :-

Sub Open_cost_xls()
Dim wbFullPath$, wbName$, p%, dirName$
wbFullPath = Application.ActiveWorkbook.FullName
wbName = Application.ActiveWorkbook.Name
p = InStr(wbFullPath, wbName)
dirName = Left(wbFullPath, p - 1)
On Error GoTo e
Workbooks.Open dirName & "cost.xls"
Exit Sub
e:
MsgBox "cost.xls could not be found in " & dirName
End Sub
 

Forum statistics

Threads
1,144,150
Messages
5,722,797
Members
422,459
Latest member
Chriselff

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
Top