2012-12-07 94 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