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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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