11

Application: Excel

Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1)

I need to go back at least 2 Folders from the Workbook Path.

I cannot use Paths like "C:/Folder1", because the Application will be moved multiple times.

Dennis
  • 183
  • 1
  • 1
  • 10

2 Answers2

17

Like this:

Function getParentFolder2(ByVal strFolder0)
  Dim strFolder
  strFolder = Left(strFolder0, InStrRev(strFolder0, "\") - 1)
  getParentFolder2 = Left(strFolder, InStrRev(strFolder, "\") - 1)
End Function


Dim strFolder
strFolder = getParentFolder2(ThisWorkbook.Path)

We here cut twice \subdir pattern...

jacouh
  • 8,473
  • 5
  • 32
  • 43
6

The FileSystemObject provides the method GetParentFolderName(path).

See How do I use FileSystemObject in VBA?

Example

Dim fso As New FileSystemObject
Dim strParent As String

strParent = fso.GetParentFolderName(Me.Range("A1").value)
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • 1
    Also works in Access. Just remember to add a Reference to the `Tools / References / Microsoft Scripting Runtime` – Murrah Jun 26 '20 at 20:53