Excel VBA用户自定义函数查找文件夹中的图像(将excel名称与图像的文件夹名称进行匹配)

目前,我正在使用一个函数来匹配图像名称从Excel表格图像文件夹,但我想要一件事…如果我保存图像,忘记在Excel中添加它的名字,那么它应该告诉我,我忘了添加名称。 例如,如果我保存图像文件夹中的3个图像

16095_1.jpg,16095_2.jpg,16095_3.jpg 

和我在Excel表格中添加图像名称

 16095_1.jpg,16095_2.jpg 

那么它应该警告我,我在Excel单元格中忘记了一个图像名称。

我的图片名称格式是 – 16095_1.jpg,16095_2.jpg

我正在使用的function是…

 Function findimage(Path As String, ImageList As String) Dim results Dim x As Long Dim dc 'double comma results = Split(ImageList, ",") If Not Right(Path, 1) = "\" Then Path = Path & "\" For x = 0 To UBound(results) results(x) = Len(Dir(Path & results(x))) > 0 Next dc = InStr(ImageList, ",,") If dc = 0 Then findimage = Join(results, ",") Else findimage = ("Double_comma") End If End Function 

此函数采用文件夹path和可变数量的模式(请参阅MSDN – 参数数组(Visual Basic) )。 使用MSDN-Dir函数遍历文件夹path中的文件名,并将其与MSDN-Like Operator(Visual Basic)中的模式进行比较,以统计与模式匹配的文件数。

用法:

  • getFileCount(“C:\ Users \ Owner \ Pictures”,“. gif”,“. png”)
  • getFileCount( “C:\用户\用户\图片”, “* GIF。”
  • getFileCount(“C:\ Users \ Owner \ Pictures”,“apple_.gif ”,“banana_.gif ”,“orange _ ##。*”)
  • getFileCount( “C:\用户\用户\图片”, “##### _#GIF。”)

 Function getFileCount(DirPath As String, ParamArray Patterns() As Variant) As Integer Dim MyFile As String Dim count As Integer, x As Long If Not Right(DirPath, 1) = "\" Then DirPath = DirPath & "\" MyFile = Dir(DirPath, vbDirectory) Do While MyFile <> "" For x = 0 To UBound(Patterns) If MyFile Like Patterns(x) Then count = count + 1 Exit For End If Next MyFile = Dir() Loop getFileCount = count End Function