0

我有一个带有一些产品 ID 代码的 Excel 表

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

我有一个存储产品图像的文件夹。图片开头有产品代码,但结尾可能不同。

有谁知道是否可以根据产品代码在外部文件夹中 Vloopup 一张图片?

4

1 回答 1

0

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

我做了一个简单的例子,虽然它可能不会像你试图做的那样做,因为我不完全确定。我希望它很容易理解。

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

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

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

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
于 2012-12-07T23:31:35.853 回答