从字符串

时间:2015-08-31 09:44:40

标签: excel-vba vba excel

我的文件位于本地文件夹中,我只需要从以下文本字符串中提取部分字符串:MasterData_Crunch 1.1.xlsx - >紧缩1.1

以下是从我的文件夹中读取所有文件到excel单元格的过程,我需要从每个文件名中仅提取上面提到的部分。所以没有MasterData_和.xlsx。我怎么能做那个programaticaly,对我来说最好,我应该如何添加到我现有的程序中以获得所需的输出。

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

非常感谢您提出的每一条建议。

1 个答案:

答案 0 :(得分:1)

For Each循环中:

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