从文件路径中检索文件夹路径

Gau*_*ikh 2 excel vba excel-vba

我可以从filedialog函数中选择文件并将文件路径存储在字符串中.但我也想要所选路径的文件夹名称.您能否告知如何从选择文件中获取文件夹路径.

选择的文件是:

U:\public\2016\Macro\CD-CW\109 file.xlsx
Run Code Online (Sandbox Code Playgroud)

我要展示直到:

U:\public\2016\Macro\CD-CW\
Run Code Online (Sandbox Code Playgroud)

我的守则

Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .AllowMultiSelect = False
    .Title = "Please select the file."
    .Filters.Clear
    .Filters.Add "Excel 2010", "*.xlsx"
    .Filters.Add "All Files", "*.*"
    If .Show = True Then
        selfile = .SelectedItems(1) 'replace txtFileName with your textbox
    End If
End With
Run Code Online (Sandbox Code Playgroud)

ror*_*.ap 7

这很简单:

Dim filePath, directoryPath As String
filePath = "U:\public\2016\Macro\CD-CW\109 file.xlsx"
directoryPath = Left(filePath, InStrRev(filePath, "\"))
Run Code Online (Sandbox Code Playgroud)

  • 不,这不仅发生了,您怎么能这么快输入:) (2认同)