2016-12-02 92 views
-3

有人可以合并这两个功能 我试图合并这两个功能合并两个VBA函数

第一功能

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 

谢胜利功能低于... 在此模式变量可以是从第一个函数的ImageList变量借助这个...

Patterns = Mid(ImageList, 1, Application.WorksheetFunction.Search("_", ImageList, 1)) & "#.jpg" 

第二个函数是...

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 

回答

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

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