VBA 03 - 应用程序路径 - 转到父文件夹

Den*_*nis 8 excel vba

应用:Excel

Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1)
Run Code Online (Sandbox Code Playgroud)

我需要从Workbook Path返回至少2个文件夹.

不能使用像"C:/ Folder1"这样的路径,因为应用程序将被多次移动.

jac*_*ouh 10

像这样:

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)
Run Code Online (Sandbox Code Playgroud)

我们在这里切两次\ subdir模式......


Chr*_*ert 6

提供FileSystemObject了方法GetParentFolderName(path)

请参阅如何在 VBA 中使用 FileSystemObject?

例子

Dim fso As New FileSystemObject
Dim strParent As String

strParent = fso.GetParentFolderName(Me.Range("A1").value)
Run Code Online (Sandbox Code Playgroud)