VBA 03 - 应用程序路径 - 进入父文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19904376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:22:30  来源:igfitidea点击:

VBA 03 - Application Path - get to Parent Folder

excelvba

提问by Dennis

Application: Excel

应用:Excel

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

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

我需要从工作簿路径中返回至少 2 个文件夹。

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

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

回答by jacouh

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...

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

回答by Christopher Peisert

The FileSystemObjectprovides the method GetParentFolderName(path).

FileSystemObject提供方法GetParentFolderName(path)

See How do I use FileSystemObject in VBA?

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

Example

例子

Dim fso As New FileSystemObject
Dim strParent As String

strParent = fso.GetParentFolderName(Me.Range("A1").value)