简体   繁体   中英

Extract words from string

I have the files in my local folder and I need to extract only part of string from the following text string: MasterData_Crunch 1.1.xlsx -> Crunch 1.1

Below is the procedure for reading all the files from my folder into excel cells and I need to extract only above mentioned part from each of file name. So without MasterData_ and .xlsx. How could I do that programaticaly, the best for me, how should I add into my existing procedure to have the desired output.

Sub ExtractFileName()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("C:\FolderWithFiles\")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 1) = objFile.Name
    i = i + 1
Next objFile
End Sub

Many thanks for each of your advices in forward.

Within your For Each loop:

Dim name As String
name = Mid(objFile.Name, 12) '12 being the length of "MasterData_"
name = Left(name, Len(name) - 5) '4 being the length of ".xlsx"

'output name
Cells(i + 1, 1) = name
i = i + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM