2012-12-07 184 views
0

我有一个Excel片与一些产品的ID码VLOOKUP在外部文件夹

100-10R 23P901 ......

我有其中产品图像被存储的文件夹。图片的开头有产品代码,但最终可能会有所不同。

有没有人知道是否可以基于产品代码在外部文件夹中Vloopup图片?

+2

VLookup自行设计用于处理工作簿中的数据,而不是文件系统。如果你要创建一个名单中的每个文件的外部文件夹的名单,那么你可以,如果你不想这样做,你需要使用VBA – NickSlash

+0

谢谢尼克。我知道VBA基础知识。这样的代码是相对复杂的,还是我应该看一下? – user1783504

回答

0

您需要查看FileSystemObject,因为这可以让您查看与FileSystem相关的东西。

我已经做了一个快速的例子,虽然它可能没有做你想做的事情,因为我不完全确定。我希望这很容易理解。

下面的代码如果放置在模块中,将使您可以访问工作表中的“checkList”函数。

checkList函数将循环遍历指定文件夹中文件名的缓存列表,检查参数“Name”是否与存储在列表中的任何项目匹配。

缓存的文件列表(FileList集合)被填充一次,第一次通过loadList子集调用checkList。

loadList子读取指定的文件夹并将所有文件名添加到集合中。

' Global Variable, used to cache the filenames 
Public FileList As Collection 

' this function returns "" if no match was found, and returns the file name if found. 
Public Function checkList(ByVal Name As String) As String 
Dim Result As String 
Dim Item As Variant 

    Result = "" 

    If FileList Is Nothing Then 
     loadList 
    End If 

    For Each Item In FileList 
     ' Performs a simple match on the filename, probably needs to be adjusted! 
     If Item Like Name & "*" Then 
      Result = Item 
     End If 
    Next Item 

    checkList = Result 

End Function 
' populates the FileList collection 
Public Sub loadList() 
Dim FSO As Object 
Dim Folder As Object 
Dim File As Object 

If FileList Is Nothing Then 
    ' First Run, needs to be created 
    Set FileList = New Collection 
Else 
    ' Should not happen unless you call it manually. 
    Set FileList = Nothing ' Clear up the old list 
    Set FileList = New Collection ' Create a new empty one 
End If 

Set FSO = CreateObject("Scripting.FileSystemObject") 
' Change "C:\" to the location of the folder you want to search 
Set Folder = FSO.GetFolder("C:\") 

For Each File In Folder.Files 
    ' Add the name to the FileList 
    FileList.Add File.Name 
Next 

End Sub