» File names and folder names using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Files, Workbook, and Worksheets in VBA
VERSION - All Microsoft Excel Versions
Function FileOrFolderName(InputString As String, _
ReturnFileName As Boolean) As String
' returns the foldername without the last pathseparator or the filename
Dim i As Integer, FolderName As String, FileName As String
i = 0
While InStr(i + 1, InputString, Application.PathSeparator) > 0
i = InStr(i + 1, InputString, Application.PathSeparator)
Wend
If i = 0 Then
FolderName = CurDir
Else
FolderName = Left(InputString, i - 1)
End If
FileName = Right(InputString, Len(InputString) - i)
If ReturnFileName Then
FileOrFolderName = FileName
Else
FileOrFolderName = FolderName
End If
End Function
Sub TestFileOrFolderName()
MsgBox FileOrFolderName(ThisWorkbook.FullName, False), , _
"This Workbook Foldername:"
MsgBox FileOrFolderName(ThisWorkbook.FullName, True), , _
"This Workbook Filename:"
End Sub
Book Store:
Recommended Books:
- Final Accounting: Ambition, Greed and the Fall of Arthur Andersen
- MP Managerial Accounting w/ Topic Tackler, Net Tutor, & PowerWeb
- Microsoft Excel Version 2002 Step by Step
- Treason: Liberal Treachery from the Cold War to the War on Terrorism
- Special Edition Using Microsoft Access 2002
- Lower Your Taxes - Big Time! : Wealth-Building, Tax Reduction Secrets from an IRS Insider
No comments have been submitted.

