Extract Excel VBA filenames from string

excel vba get filename from path without extension
excel vba get filename from folder
extract folder name from path in excel
excel vba get folder name from path
vba file path string
excel vba get file name
excel get path from filename
getfilename vba

I want to extract file names from a string. The length of the string and the length of the file name are always different. Must be done with VBA!


href ist gleich: "abc/db://test.pdf|0|100">Bsp.:

I would like that: test.pdf

I do not know how to proceed. It would also be nice if the script could extract multiple filenames from a string.



href ist gleich: "abc//db://test.t.pdf|0|100" "db://test1.pdf|0|100">Bsp.

I would like that:

test.t.pdf test1.pdf
Sub testExtractFileName()
 Debug.Print extractFileName("file://D:/ETVGI_556/Carconfigurator_file/carconf_d.pdf", "//")
 Debug.Print extractFileName("abc//db://test.t.pdf|0|100")
 Debug.Print extractFileName("db://test1.pdf|0|100")
End Sub

Function extractFileName(initString As String, Optional delim As String) As String
 Dim necString As String
   necString = left(initString, InStr(initString, ".pdf") + 3)
   necString = Right(necString, Len(necString) - InStrRev(necString, _
            IIf(delim <> "", delim, "/")) - IIf(delim <> "", Len(delim) - 1, 0))
   extractFileName = necString
End Function

The single condition is that in front of the file name (all the time) to exist "//" characters in the initial string. And of course the file extension to all the time to be .pdf. If not, this extension is required and the function can be easily adapted... The function returns full name if the second (optional) parameter will be "//" or just the file name (without path) if it is omitted.

Quickly Get (Extract) Filenames from FilePaths in Excel using VBA , Quickly Get (Extract) Filenames from FilePaths in Excel using VBA Macro objFSO = CreateObject("scripting.filesystemobject") Dim fileName As String fileName� The VBA InStr function is one of the most used functions in VBA. It is used to find a string within a string and indeed it does a very fine job. However, it is often used to help extract part of a string and for this task it performs badly. If you have found string extraction in VBA to be a painful process, then read on. This post will show you

One option could be using a pattern where you would match the preceding / and capture in a group 1+ word characters \w+ followed by .pdf

Your value is in capturing group 1.


See a regex demo

If you want to have a broader match than \w you could extend what you do want to match using a character class or use a negated character class [^ to match any char except the listed in the character class.

In this case the negated character class [^/|"\s] would match any char except / | " or a whitespace character \s


See another regex demo

Extracting file name and folder name from the file path using VBA in , For using defined custom function, go to cell C14 and enter the function =FileOrFolderName(B14,FALSE) and in cell D14, enter the function =FileOrFolderName(B14,TRUE), where cell B14 contain the file path. Cell C14 will give the folder name and Cell D14 will give the file name. Extract Filenames from Filepath in Excel Getting filenames from a Filepath looks an easy task at first sight. But actually this is a very cumbersome task when you are dealing with large number of file paths.

Try this and edit it according to your needs. At least it was designed for two of your examples.

Dim sStringToFormat As String
Dim i As Integer
Dim vSplit As Variant
Dim colFileNames As Collection
Dim sFormattedString As String

Set colFileNames = New Collection

sStringToFormat = "href ist gleich: ""abc//db://test.t.pdf|0|100"" ""db://test1.pdf|0|100"">Bsp."

vSplit = Split(sStringToFormat, "/")

For i = LBound(vSplit) To UBound(vSplit)

    If InStr(vSplit(i), ".") > 0 Then
        sFormattedString = Split(vSplit(i), "|")(0)
        sFormattedString = Split(sFormattedString, "<")(0)
        sFormattedString = Split(sFormattedString, ">")(0)
        colFileNames.Add sFormattedString
    End If

Next i

How to quickly extract filename from full path in Excel?, In Excel, you can use the below formula to quickly extract only file name from the full path. Function FunctionGetFileName(FullPath As String ) As String Besides the User Defined Function, a VBA code also can help you to extract the� I am trying to fetch excel book filenames from folder that contains the excelbook I am working on and contain a specific string like *WFM*.xl*. I receive few files daily from WFM dept but file names have "WFM" appear at different location within file name and since these are daily reports they have dates attached to the file names as well.

Retrieving The File Name & Extension From A File Path String , 'Retrieve File Name with Extension (Displayed in Immediate Window answers to complex problems in Excel, PowerPoint, VBA, you name it! hoping someone could help, hoping I could automate my excel list using VBA or other procedure, For A, the idea is I have a PDF file, let say rev. 1,2,3,4 etc, and I will put it in one folder, what I need is I need to capture the latest revision with hyperlink using formula.

Capture Part of a File Name, Function CustName() As String Dim lngStart As Long Dim lngEnd As Long 'find Name, 1) 'pull customer name out of the file name CustName Learn Excel - Changing Case in Excel: Lower, Upper, Proper: Podcast #� Extract Numerical Portion of String The following function will extract the numerical portion from a string: Function Extract_Number_from_Text(Phrase As String) As Double Dim Length_of_String As Integer Dim Current_Pos As Integer Dim Temp As String Length_of_String = Len(Phrase) Temp = "" For Current_Pos = 1 To Length_of_String If (Mid(Phrase, Current_Pos, 1) = "-") Then Temp =…

Extracting File Names from a Path (Microsoft Excel), This function was introduced in the version of VBA provided with Excel 2000, and it will pull a string apart based upon a delimiter you specify� Excel VBA String Functions. VBA String Functions does not replace the string but the result of this functions creates the new string. There are many string functions in VBA and they are all categorized under string or text functions, some of the important functions are LEFT function to get the value from left and RIGHT function to get the value from right or the MID function, LEN and INSTR

  • Does the name always have // before and | after?
  • You might use a regex like db://([^|"\s]+\.pdf)\| and take the value from capturing group 1. See regex101.com/r/QoOiXZ/1
  • No another example could be: leich: "file://D:/ETVGI_556/Carconfigurator_file/carconf_d.pdf">carconfig
  • Then try /([^/|"\s]+\.pdf) regex101.com/r/rad6lx/1 or /(\w+\.pdf) regex101.com/r/zb4ISt/1
  • Try looking for .pdf in the string and then look for a ` in the string before the .pdf`..?
  • I do not think he would want to test both strings at ones. I am afraid it was just a bad way of his need definition...
  • he needed to return all file names with extensions from string. not only the first match
  • I mean you may be right, but I couldn't understand what exactly he needs. I (just) believe that he needs to extract the path from each such a string. A string, not what he presented. But I may be wrong, too... I think, now it is this turn to clarify the issue.
  • You probably changed the variable name at the end and you forgot to change vSplit = Split(s, "/") in vSplit = Split(sStringToFormat, "/"). I think it's good to edit it and correct...