3

I need to go from absolute to relative paths.

My macro is in folder \models\vba

and this macro uses sheets from the folders \models\Main ,and \models\aear as below

 Set Main = Workbooks.Open("D:\Shared\Models\Main\Main 2017.xlsx")

I've been shopping around already, and it seems its easiest to use

ThisWorkbook.Path

What's the best way to go back one directory, and then enter the Macro map.?

2
  • How is the D:\Shared directory related to the location of the workbook file?
    – Dai
    Feb 3, 2017 at 5:48
  • Is your project comprised of a single *.xlsx/*.xls workbook file, or multiple files? How is your "macro in a folder"?
    – Dai
    Feb 3, 2017 at 5:54

1 Answer 1

7

You can construct relative paths the usual way but you need always to append them to ThisWorkbook.Path.

Set Main = Workbooks.Open(ThisWorkbook.Path & "\..\Main\Main 2017.xlsx")

Each .. goes one step up in the folder tree.

You can eventualy encapsulate this by building your own function that constructs the absolute path from your relative path

Function RelToAbs(RelPath as string) as string
    RelToAbs = ThisWorkbook.Path & "\" & RelPath
End function

Set Main = Workbooks.Open(RelToAbs("..\Main\Main 2017.xlsx"))
4
  • 1
    post the path of your start folder and post the path of your target folder.
    – teylyn
    Feb 3, 2017 at 6:23
  • start folder D:\Shared\Models\vba ___ target folders D:\Shared\Models\Main D:\Shared\Models\aear
    – Ken
    Feb 3, 2017 at 6:27
  • 2
    @Ken : if ThisWorkbook.Path is D:\Shared\Models\vba, ThisWorkbook.Path & "\.." is D:\Shared\Models... and ThisWorkbook.Path & "\..\Main is D:\Shared\Models\Main
    – A.S.H
    Feb 3, 2017 at 6:30
  • 1
    Wait it, actually did work, i messed up the addressing myself for a sec. @ ASH, Correct. i messed up. Thanks mate !
    – Ken
    Feb 3, 2017 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.