简单例子:
Function CopyPicture(rng1 As Range, patharg As String, pathargNew As String) As String
Dim r1 As Range
Dim total As Long
Dim success As Integer
Dim needStr As String
Dim MyFile As String
Dim isSucess As Integer
Dim copyNum As Integer
total = rng1.Rows.Count
If Right(patharg, 1) <> "\" Then
patharg = patharg & "\"
End If
If Right(pathargNew, 1) <> "\" Then
pathargNew = patharg & "\"
End If
For Each r1 In rng1 '循环当前选中列
isSucess = 0
needStr = "*" & r1 & "*.jpg" 'like通配符
MyFile = Dir(patharg & needStr) '获取第一个文件
If MyFile Like needStr Then
success = success + 1
FileCopy patharg & MyFile, pathargNew & MyFile
copyNum = copyNum + 1
End If
If isSucess = 0 Then
Do While MyFile <> ""
MyFile = Dir '第二次读入的时候不用写参数
If MyFile = "" Then
Exit Do '当MyFile为空的时候就说明已经遍历完了,这时退出Do,否则还要运行一遍
End If
If MyFile Like needStr Then
success = success + 1
copyNum = copyNum + 1
FileCopy patharg & MyFile, pathargNew & MyFile
If copyNum > 4 Then
Exit Do
End If
End If
Loop
End If
Next
CopyPicture = "总共选择了" & total & "个产品,复制了" & success & "张图片"
End Function