VBA to open workbook in different folder, without knowing full path

Richard_mcr

New Member
Joined
Oct 19, 2023
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hello Again!

I'm looking for some VBA to open a worksheet in a folder from a workbook in a different folder, however the full path will vary depending on who's copied the files where. As an example, I have a group of folders which always maintain the same structure, but are copied into different parent folders. Therefor, I need to establish the file path of the main workbook, go up a folder level, and then select the known folder name and then the worksheet. I've tried to show it visually here:

1698612425882.png

As always, any help is much appreciated!

Thanks!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here is a snippet I use in these cases where 'LocalPath' would be your different local path; see if you can use it in your project:
VBA Code:
Sub RetrieveLocalPath()
    Dim FilePath As Variant
    Dim PathOnly As String
    Dim LocalPath As String
    PathOnly = ThisWorkbook.Path                  'retrieve Main WB's path
    FilePath = Split(PathOnly, "\")
    ReDim Preserve FilePath(UBound(FilePath) - 1) 'shift up 1 subfolder (or more)
    LocalPath = Join(FilePath, "\") & "\.DWG"     'new local path
    Debug.Print LocalPath                         'for testing purposes, shows only in the Immediate panel
End Sub
 
Last edited:
Upvote 0
Solution
If you know the file structure, I was going to suggest:

VBA Code:
Sub openWorkbook(workbookName As String)

    Dim sourceWorkbook As Workbook
    Dim folderPath As String
    Dim filePath As String
    
    folderPath = Application.ThisWorkbook.Path

'  The following assumes you would like to open the workbook to retrieve the worksheet

    Set sourceWorkbook = Workbooks.Open(Left(folderPath, InStrRev(folderPath, "\")) & ".DWG\" & workbookName)


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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