Macro to extract file name from file path [Excel VBA]

Excel VBA macro to extract file name from file path. This macro extracts the string (file name) which is between dot (.) and backslash(\) delimiters (from right side).

Sub extractFileNameFromFilePath()
' This macro shows you how to extract filename from filepath
' It extracts the filename which is between delimiters dot and backslash
filePath = "C:\Users\Desktop\files\file1.html"
firstDelPos = InStrRev(filePath, ".")
secondDelPos = InStrRev(filePath, "\")
Filename = Mid(filePath, secondDelPos + 1, firstDelPos - secondDelPos - 1)
MsgBox (Filename)
End Sub

About the Author


A passionate blogger. Love to share solutions and best practices on wordpress hosting, issues and fixes, excel VBA macros and other apps

Leave a Reply

Your email address will not be published. Required fields are marked *