VB Code for a directory

M

Marie Bayes

Hi
Is there any code I can put into my macro (which opens a file) that will
always look for the file in the 'current' directory, ie, the directory that
the original file was opened in, so that if I move the directory it will
always find it?
Thanks in advance
 
R

RichardSchollar

Hi Marie

You need the following where you have the workbook open statement:

Workbooks.Open ThisWorkbook.Path & "\" & YourWorkbookName

Then this will always open the file named "YourWorkbookName" in the
same directory as the workbook that holds the macro code.

Hope this helps!

Richard
 
B

Bob Phillips

Workbooks.Open ThisWorkbook.Path & Application.PathSeparator & Workbook.Name

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
M

Marie Bayes

So, will this statement need to be at the start of each of the macros, the
macros open several files so will I need to type this in for each file that
will be opened? And then in the line of code that opens the workbook, will
it look like this;

Workbooks.Open Filename:= _
"\myfile.xls"

Thanks
 
M

Marie Bayes

Thanks Richard

RichardSchollar said:
Hi Marie

You need the following where you have the workbook open statement:

Workbooks.Open ThisWorkbook.Path & "\" & YourWorkbookName

Then this will always open the file named "YourWorkbookName" in the
same directory as the workbook that holds the macro code.

Hope this helps!

Richard
 
B

Bob Phillips

No, just make sure that you precede each file open with the code as shown
earlier. You could change the directory, but I personally prefer to be
explicit, not rely on being in any particular directory.

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
J

JLatham

Marie,
This should not affect you since you're opening an existing file. But when
you start a NEW file (File | New) it has no path, since it hasn't been saved
to disk yet. Saving such a file in code results in a save to the current
directory (usually your My Documents folder).

Just thought I'd mention that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top