I am given a complete path of a file like "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls". Now from this string, I need to extract only the file name i.e. 'xyz.xls'.
Is there any VBA function to accomplish that?
Thanks for your help.
I am given a complete path of a file like "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls". Now from this string, I need to extract only the file name i.e. 'xyz.xls'.
Is there any VBA function to accomplish that?
Thanks for your help.
Parse the string with SPLIT():
Sub dural() Dim s As String s = "C:\bigfolder\mediumfolder\smallfolder\tinyfolder\microfolder\temp.xls" ary = Split(s, "\") MsgBox ary(UBound(ary)) End Sub
Last edited by Aussiebear; 12-20-2022 at 12:07 PM. Reason: Added code tags
Have a Great Day!
One more:
Public Sub GetFileName() Dim strPath As String, strFile As String strPath = "c:/folder1/subfolder-2/.../subfolder-n/xyz.xls" strFile = Mid(strPath, InStrRev(strPath, "/") + 1, Len(strPath)) MsgBox strFile End Sub
Last edited by Aussiebear; 12-20-2022 at 12:08 PM. Reason: Edited code tags
Regards,
--------------------------------------------------------------------------------------------------------
Shrivallabha
--------------------------------------------------------------------------------------------------------
Using Excel 2016 in Home / 2010 in Office
--------------------------------------------------------------------------------------------------------
You must be kidding:
Your first lesson/chapter in VBA manipulating files/folders.msgbox dir("c:/folder1/subfolder-2/.../subfolder-n/xyz.xls")
See also:
http://www.snb-vba.eu/VBA_Bestanden_en.html
Last edited by Aussiebear; 12-20-2022 at 12:08 PM. Reason: Edited code tags