简体   繁体   English

Excel VBA:如何从文件路径中提取特定的文件夹

[英]Excel VBA: How to extract specific folder from file path

I'm looking for a function that will return the fourth file within a file path. 我正在寻找一个函数,该函数将在文件路径中返回第四个文件。 For example, say my file path is "C:\\Users\\Desktop\\Programs\\Training Log\\Folder Database" and I would like to extract just the "Training" folder and save as variable. 例如,假设我的文件路径为“ C:\\ Users \\ Desktop \\ Programs \\ Training Log \\ Folder Database”,而我只想提取“ Training”文件夹并另存为变量。 How could I go about doing that? 我该怎么做呢?

Here is my current function: 这是我当前的功能:

Function GetFilenameFromPath(ByVal strPath As String) As String

    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

This function just returns the immediate parent folder. 此函数仅返回直接父文件夹。 How could I tweak it to specify a folder other than the immediate parent. 我该如何调整它以指定直接父级以外的文件夹。

Thanks!! 谢谢!!

You can use Split to get the 4th part. 您可以使用Split获得第4部分。

Sub Sample()
    Dim FilePath As String
    Dim MyAr As Variant

    FilePath = "C:\Users\Desktop\Programs\Training Log\Folder Database"
    MyAr = Split(FilePath, "\")

    Debug.Print MyAr(4)
End Sub

You can accomplish this using this function: 您可以使用以下功能完成此操作:

Public Function getParentFolder(path As String, Optional level As Integer = 0)
    Dim pathTokens()    As String
    pathTokens = VBA.split(path, "\")
    Debug.Assert level >= 0 And level <= UBound(pathTokens)
    getParentFolder = pathTokens(UBound(pathTokens) - level)
End Function

By using Optional the default value is 0, so you can use this as a single- or two-argument function. 通过使用Optional ,默认值为0,因此您可以将其用作单参数或二参数函数。 The level argument lets you choose how many levels (directories) deep you go. level参数使您可以选择要深入的级别(目录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA从文件夹打开xlsx文件而不编写路径 - Excel VBA Open xlsx File From Folder Without writing Path 如何将特定文件从子文件夹复制到目标文件夹? (Excel VBA) - How to copy specific files from subfolders to a destination folder? (Excel VBA) Excel VBA-如何提取特定行 - Excel VBA-How to Extract Specific Rows 如何使用VBA从xml文件的CDATA部分提取特定信息? - How to extract specific information from CDATA section of an xml file with VBA? 如何在文本文件中提取路径并在VBA中使用它? - How to extract a path in text file and use it in VBA? 我如何使用excel vba将网站上的特定数据(名称,详细信息)提取到excel中? - How do i extract specific data (name, details )from the website into excel with excel vba? 如何返回一个文件夹中所有文件的文件路径? 这是用于Excel VBA - How can I return the file path of all files, within one folder? This is for Excel VBA Excel VBA-从同一文件夹中的文件导入,并带有任意名称,但末尾有特定编号 - Excel VBA - Import from a file in the same folder with an arbitrary name but a specific number at the end 使用vba从Excel-Workbook单元中读取文件夹的路径 - Read a path to a folder from a Excel-Workbook cell using vba 从单元格导入数据的Excel VBA文件夹路径 - Excel VBA Folder Path from Cell to import data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM