Question : Change the Current Directory to the Directory where the Excel Macro was run from

Hi Guys!

I need a Microsoft Excel 2000 macro that will set the Current Directory to the directory from which the .xls file which contains it is run. The Current Directory is that directory which the ActiveWorkbook.SaveAs saves to and the Workbooks.Open opens from and I'm assuming the Application.GetOpenFilename opens up into.

An adequate test harness would be to have it called from the Workbook_Open subroutine and then run Application.GetOpenFilename to demonstrate that the macro knows which directory its workbook is in. The workbook should be able to be moved to a different directory or Excel should be able to save something to a different directory (thereby changing the current directory) and still work.

Answer : Change the Current Directory to the Directory where the Excel Macro was run from

HEllo DBabbit,

you can look into these

CurDir *will* give you what Excel sees as the current directory - if you use ChDir to change the directory (or open a file from a directory other than the default directory), CurDir will be updated.  Here's an example I copied from my Immediate window:

    ?curdir
    C:\Temp\
    chdir "C:\"
    ?curdir
    C:\

I am guessing that you are looking for the directory that a particular workbook is stored in.  For this, check out the Path and Name properties of the Workbook object.  If you want to get the directory or name of the active workbook, use this:

    ActiveWorkbook.Path
    ActiveWorkbook.Name

If you wish to get the directory or name of the workbook that contains the VBA code you are writing, use this:

    ThisWorkbook.Path
    ThisWorkbook.Name

HAGD:O)Bruintje
Random Solutions  
 
programming4us programming4us