我能够从文件对话框功能中选择文件并将文件路径存储在字符串中。但我也想要所选路径的文件夹名称。你可以请告知如何从选择文件中获取文件夹路径。选择从文件路径检索文件夹路径
文件是:
U:\public\2016\Macro\CD-CW\109 file.xlsx
我想说明,直到:
U:\public\2016\Macro\CD-CW\
我的代码
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
的复制(http://stackoverflow.com/questions/42462625/how-to-remove-the-last-element-from-a- [如何从VBA的路径删除最后一个元件] path-in-vba) –
此问题已经有答案[这里](http://stackoverflow.com/a/42462687/4926357) –
[使用'Scripting.FileSystemObject'](http://stackoverflow.com/文档/ VBA/990 /脚本,FileSystemObject的/ 11587 /检索,只有最路径从-A-文件路径)。它比解析文本更不容易出错。 – Comintern