?
Solved

Get directory path filename in VBA

Posted on 2012-09-06
3
Medium Priority
?
845 Views
Last Modified: 2012-09-06
I have a column in an Excel spreadsheet that has around 40k records in it, the column is part of a much larger spreadsheet with multiple columns. Each row in the spreadsheet has a varying directory path...

I am looking for a way in VBA to clear out the directory path leaving just the filename (preferably without the file extension). Belowut this runs  I found to try and acheive this - but this runs very very slowly and is not an option..

Does anyone know a quick way of achieving this?

Function GetFilenameFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

Open in new window

0
Comment
Question by:Blowfelt82
3 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 38371816
Can you just use the Find/Replace dialog (ctrl+h) and enter:
*\
in the Find box, leave the replace box blank, and press Replace All?
0
 
LVL 7

Accepted Solution

by:
Ironhoofs earned 2000 total points
ID: 38371883
Recursively calling GetFilenameFromPath() will indeed take up a lot of time. Try this instead:

Function GetFilenameFromPath(ByVal strPath As String) As String
    Dim pos As Integer

    pos = InStrRev(strPath, "\")
    If pos > 0 Then
        strPath = Mid(strPath, pos + 1)
    End If
    
    GetFilenameFromPath = strPath

End Function

Open in new window

0
 

Author Closing Comment

by:Blowfelt82
ID: 38372010
Worked like a charm!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question