VBA Code To Separate Filename From The Path

Dim fullPath() As String
Dim wbIndex As Long
Dim wbName as String
Dim wbAddr as String

‘ Wbaddr holds the full path for example: ” D:\Documents and Settings\Desktop\Preeti\macro\Input.xls”

fullPath() = Split(wbAddr, “\”) ‘PutĀ  Parts of the path into the array fullPath()
wbIndex = UBound(fullPath) ‘wbIndex holds the largest subscript
wbName = fullPath(wbIndex) ‘wbName holds the filename i.e. Input.xls

Here, through Split( ) we have stored the srings separated by “\”. The UBound( ) gives the maximum subscript value through which we get the filename.