使用基于前一个单元格的path摘录的文件名填充单元格

我正在使用一个excel电子表格,这个电子表格有A列中的标题列表和D列中的文件名列表。这些文件名是根据列A中标题的特定命名约定创build的,例如if A1中的标题是“Taco_123”,那么D1中的文件名将包含精确的短语“Taco_123”。

目前,文件创build后,文件名必须手动input到D1中。 但是我想要的是一个可以自动完成的VBA脚本。 我的想法是,每行都会读取列A中的标题,在文件所在的目录中search包含该确切短语的文件,然后将文件名(减去扩展名)复制到D列中。

这样的事情甚至可能吗? 我试着在网上search一个解决scheme,因为我知道关于VBA的zilch,但是我没有太多的运气。 我什至不能find有同样问题的其他人。 任何帮助,将不胜感激。

编辑:我不知道这是否有所作为,但文件types,需要search匹配的文件名是PSD。

这样的事情绝对有可能。 有一个简单的方法可以在Excel,旧版本的Excel中使用Application.FileSearch来执行此操作。 对于2007年和更新版本,您将不得不使用或修改P.Havdra的解决scheme,在这里:

http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/a450830d-4fc3-4f4e-aee2-03f7994369d6/

这个脚本打印出一个与您的search参数相匹配的文件列表,也给你一个恼人的消息框popup。

我对他的FileSearch子例程做了一些修改,让你开始。 这将在指定的目录中生成一个匹配PSD文件名的数组,然后您可以使用该数组进行操作并与工作表数据进行交互。

 Sub FileSearch() ' ' Example of FileSearchByHavrda procedure calling as replacement of missing FileSearch function in the newest MS Office VBA ' 01.06.2009, Author: P. Havrda, Czech Republic 'As modified Feb-13-2013, David Zemens, for _ http://stackoverflow.com/questions/14865258/populate-cell-with-a-filename-based-on-path-excerpt-in-previous-cell ' Dim sDir As String '< this is the search directory you will use Dim FileNameWithPath As Variant Dim ListOfFilenamesWithParh As New Collection ' create a collection of filenames Dim rCount As Long 'row counter Dim myArray() As Variant Dim a As Long 'array iterator a = 0 'Set the search directory: sDir = "C:\DESKTOP\" '<-- MODIFY TO SUIT YOUR NEEDS ' Filling a collection of filenames (search Excel files including subdirectories) Call FileSearchByHavrda(ListOfFilenamesWithParh, sDir, "*.psd", False) ' Print list to immediate debug window and as a message window For Each FileNameWithPath In ListOfFilenamesWithParh ' cycle for list(collection) processing 'Create an array of matching filenames ReDim Preserve myArray(a) myArray(a) = FileNameWithPath a = a + 1 Next FileNameWithPath ' If no file was found: If ListOfFilenamesWithParh.Count = 0 Then 'Debug.Print "No file was found !" MsgBox "No file was found !" Else: 'some files were found, so do something with myArray '######################################################## ' ' <-- Code to manipulate your worksheet will go here --> ' ' '######################################################## End If End Sub