» 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:
- Cashflow Quadrant: Rich Dad's Guide to Financial Freedom
- Microsoft Access Version 2002 Step by Step
- The McGraw-Hill Guide to Writing a High-Impact Business Plan: A Proven Blueprint for First-Time Entrepreneurs
- Monte Carlo Methods in Finance
- Microsoft Word Version 2002 Inside Out
- Business Analysis and Valuation: Using Financial Statements, Text and Cases
No comments have been submitted.


