Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve folder path from file path

Tags:

excel

vba

I am able to select the file from filedialog function and storing the file path in string. but I also want the folder name of the selected path. Can you please advise as to how to get the folder path from select file.

File selected is :

U:\public\2016\Macro\CD-CW\109 file.xlsx

I want to show till :

U:\public\2016\Macro\CD-CW\

My Code

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
like image 846
Gaus Shaikh Avatar asked Dec 18 '22 09:12

Gaus Shaikh


1 Answers

This is very simple:

Dim filePath, directoryPath As String
filePath = "U:\public\2016\Macro\CD-CW\109 file.xlsx"
directoryPath = Left(filePath, InStrRev(filePath, "\"))
like image 129
rory.ap Avatar answered Feb 16 '23 19:02

rory.ap